Home > Enterprise >  Regex - Negotiate capture group
Regex - Negotiate capture group

Time:09-02

I'm not a regex expert. I have the following regex, which seems to work fine, to get a number with 2 decimal values using as decimal splitter . or ,.

/([0-9]*[\.|\,]{0,1}[0-9]{0,2})/g

This should be the behaviour:

1 => should be valid
1.1 => should be valid (same with using `,` instead of `.`)
1.23 => should be valid (same with using `,` instead of `.`)
1.235 => should be invalid
-1 => should be invalid 
abc => should be invalid 
1a => should be invalid ...

A regex to test, you can find here:

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

But my issue is now, to negotiate the whole regex expression. I want to get all which is not matching the regex. I already tried this:

^(?!([0-9]*[\.|\,]{0,1}[0-9]{0,2}))

Can anyone help? Many thanks in advance!

CodePudding user response:

I want to get all which is not matching the regex.

If you want to match all the words that don't match number pattern then use:

(?<!\S)(?!\d (?:[.,]\d{0,2})?)\S 

Breakup:

  • (?<!\S): Lookbehind to assert that previous char is not a non-whitespace
  • (?!\d (?:[.,]\d{0,2})?): Negative lookahead to assert that we don't have a numbered text ahead of us
  • \S : Match 1 non-whitespace RegEx Demo 1

Or if you want to match full line that don't contain this number text then use:

^(?!.*\b\d (?:[.,]\d{0,2})?). 

RegEx Demo 2

  • Related