How to match decimal numbers with no leading zeros?

Viewed 69

I'm dealing with a regex in Java that should capture all occurrences of decimal numbers with no leading zeros.

Example:

The cat is .75 high but the dog id 3.67 high instead. All animals aren't higher that .87.

I expect to capture only .75 and .87 as they are decimals with whatever numbers of digits, but without the leading zero. I should not capture 3.67 instead.

I tried capturing it with word boundaries on both sides:

\b\.\d+\b

But the word boundary on the left side of this doesn't work well. Without the word boundary, it matches 3.67 too.

What would be the correct regex syntax to achieve this requirement?

1 Answers

You want to match the opposite of \b at the start of the pattern. For this, there is uppercase \B, which matches where \b doesn't.

Basically, you're looking for a decimal point that is not at a word boundary (because there would be a word boundary between the decimal point and any other numbers), followed by numbers, followed by a word boundary.

\B matches at every position where \b does not. Effectively, \B matches at any position between two word characters as well as at any position between two non-word characters.

\B\.\d+\b

See this demo at regex101

Related