Ranju
Ranju

Reputation: 157

Regular Expression to find amount without trailing % sign

Need a regular expression to get the amount (892.33) from the below text

Property Tax Allotment 3.76% 892.33 USD

Condition - It should not consider numbers with trailing % sign

Am trying with the below pattern

\\s?[0-9\\.,]{2,20}(?!\\%)\\s?

but getting 3.76 without % sign

Upvotes: 0

Views: 72

Answers (2)

Michael
Michael

Reputation: 44200

How about this:

\d+\.\d\d(?= USD)

One or more digits \d+ followed by a literal dot \. and two more digits \d\d, with a positive lookahead for " USD" (?= USD)

It would need to be modified slightly if the fractional component of the price is optional.

Try it online.

Upvotes: 1

revo
revo

Reputation: 48751

Add a negative lookahead for any following digits:

\\b[0-9.,]{2,20}(?!\\d)(?!%)\\b

Live demo

Upvotes: 2

Related Questions