user2716722
user2716722

Reputation: 93

Regex match a number the is not

I am trying to get syntax parsing for a custom file format working. I am looking to highlight only numbers but the numbers can be in several types of formats. Although the numbers should not be in a variable name or word of some sort.

To keep things simple lets say I am looking for any number of this type [0-9][\.0-9]* and I am looking to keep just the number and not it's padding. However the only problem is that this picks up more cases than I want it to. Theses are some of the fail cases that I am looking to avoid variable123 or variable_123name or _123_, where some of the acceptable cases are |123| or +123+ $123 123% ext... 11 2123 123% 0.12 1.1.3.4.4 12 1 23452 23423| ext...

I am basically looking to only get rid of the unnecessary variable highlighting while keeping numbers highlighted in a more relaxed case where these numbers can be in lists surrounded by many other random characters. I have tried with lookaheads with several examples from this site but have come up with no good solution. I have a few fail criteria and I want the hilighting criteria to be loose. There are a lot of numbers but the only important cases to get rid of is when the number is imbedded into an id tag typically like a variable decleration. This means the string should have no letters in it [a-zA-Z] as well as no underscores [_a-zA-Z] and tried to use this schema to eliminate those cases using lookup. Although this didn't solve my problem. Here is a link to my problem (regex101.com).

Upvotes: 0

Views: 62

Answers (2)

user557597
user557597

Reputation:

update

After re-reading your question, it should be able to be done with this
https://regex101.com/r/K6fQXy/1

(?<!\w)\d[.\d]*(?!\w)

Formatted

 (?<! \w )
 \d [.\d]* 
 (?! \w )

Or, if you don't want to match a trailing dot after digits, this one
https://regex101.com/r/K6fQXy/2

(?<!\w)\d(?:\.?\d)*(?!\w)


Just roll your own regex.
Use assertions to qualify the digits to highlight.

https://regex101.com/r/XwDEkj/1

(?<=\|)\d+(?=\|)|(?<=\+)\d+(?=\+)|(?<=\$)\d+|\d+(?=%)

Formatted

     (?<= \| )
     \d+ 
     (?= \| )
  |  
     (?<= \+ )
     \d+ 
     (?= \+ )
  |  
     (?<= \$ )
     \d+ 
  |  
     \d+ 
     (?= % )

Upvotes: 1

NetMage
NetMage

Reputation: 26927

If available, use the word border matches

\<[0-9][\.0-9]*\>

Upvotes: 0

Related Questions