Java: In a string, how to extract a number that precedes a regex match?

I have a string that contains a fraction of 100. The string looks like this:

95 / 100
1.4GB / Unlimited

I want to get the value that is before “ / 100”. In the above case, this value would be 95. But it can be any number between 0 and 100.

I know that if I use the following regex expression matches the “ / 100” portion of the string. What I need to know is how to get the number that precedes this match.



You can use a positive lookahead in order to make digits preceded by that pattern.

In the expression below, “95” would be in the capturing group.

\b(\d+)\b(?= \/ 100\b)


\b - Word boundary to ensure that the digit isn’t surrounded by any other characters.


(\d+) - Capturing group to match one or more digits.


(?= \/ 100\b) - Positive lookahead to match the preceeding digits if they are followed by / 100.

But it can be any number between 0 and 100.

If the number has to be between 0 and 100, use could also use the following:

\b(\d{1,2}|100)\b(?= \/ 100\b)
  • \d{1,2}|100 matches a number between 0-100
  • The rest is the same as the example above.