Regex for finding a distinct (discrete) number-percentage occurrence in a file

0 votes

As an example, I'm trying to search for distinct (discrete) instances of '20%' in the text of a file using FileLocator Pro.

Text example in file: "I have an interest rate of 20%"

I'm trying to find a way to search for '20%' as a discrete result.

I've tried ^20%$ with a small number of results when there should be more.

\b20% throws up instances of '20%' but also instances where it exists within any other number (say, in '1.20%'). \b20%\b doesn't give any results.

I'm fairly new to all this, so please forgive my ignorance - help much appreciated!

asked Nov 28, 2016 by JMowatt (40 points)

1 Answer

+1 vote

I'm not sure if you'd want "The error range is 10-20%" but assuming that would be OK try something like this:

(?<![^0-9.])20%

This regex uses a negative look behind to check that the previous character is not a digit (ie 0-9) or a decimal point.

answered Nov 28, 2016 by dave (52,970 points)
Many thanks for taking the time to answer.

Since I posted, I came to a very similar solution; I used (?<![-.])\b20% which seemed to work. I don't think we need the caret as is in your solution, but the addition of the digit range is something I'll incorporate.

Many thanks again.
...