Exclude a set of specific numbers in the regex pattern "\ d +"

Say I have a few lines:

some50 some51/x some52/y some53/z some510 some511/x some512/y some513/z 

I want to check if my string is in the form some + number + anything.

I use the template for this requirement: ^some\d+ , which returns all matches to me.

Now I have a requirement to exclude some of the numbers. For example, I must exclude 51 and 52 from matches. What is the correct expression?

I tried (rather a guess than an attempt that I admit):

  • ^some(\d+|(?!(51|52)))
  • ^some((\d+)(?!(51|52)))
  • ^some(\d+)^(?!(51|52)))
  • ^some(\d+)(?!(51|52)))

but not one of them returns the expected result:

 some50 some53/z some510 some511/x some512/y some513/z 

PS: I don’t know if this matters, but the regular expression is used in the PowerShell script.

[Change] . Expanded questions to show that 51x should not be ruled out

+4
source share
2 answers

You are looking for a negative statement. I am not familiar with the PowerShell script, but in a perl-compatible regular expression this works:

 ^some(?!(51$|52$))\d+$ 

This also does not match some510 . If this is not intended, delete $ after 51 and 52

update: go from line boundary to word boundary (at the end of the line)

 ^some(?!(51\b|52\b))\d+\b 
+4
source

Here is one way, perhaps not the most elegant: ^some([0-46-9]\d*|5([03-9]|\d\d+)|5)

-1
source

Source: https://habr.com/ru/post/1435506/


All Articles