4

Let's say I have some strings :

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

I want to test if my string are in the form some + a number + anything.

I use for this requirement the pattern : ^some\d+, which is returning me all matches.

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

I've tried (more a guess than a try I admit):

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

but none of the them are returning the expected result, which is :

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

PS: don't know if it matters, but the regex is used within a PowerShell script.

[Edit] Extended the questions to show that 51x should not be excluded

Steve B
  • 36,818
  • 21
  • 101
  • 174

2 Answers2

4

You are looking for a negative lookahead assertion. I'm not familiar with PowerShell script, but in perl compatible regex this works:

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

This will also not match some510. If that's not intended, remove the $ after 51 and 52

update: changing from string-boundary to word-boundary (at the end of the string)

^some(?!(51\b|52\b))\d+\b
mvds
  • 45,755
  • 8
  • 102
  • 111
  • 2
    According to http://www.regular-expressions.info/refflavors.html, PowerShell uses the .NET flavor of regular expressions, and so lookaheads are indeed supported. +1 – Andrew Cheong Sep 21 '12 at 14:07
  • This is nearly working. I removed the $ because there is leading characters. The regex is then `^some(?!(51|52))\d+` The only remaining issue, is that 510 or 520 is also excluded. I've updated my question to show what I meant. – Steve B Sep 21 '12 at 14:16
  • 1
    @SteveB: ok, then change the `$` to `\b` to match word boundaries. – mvds Sep 21 '12 at 14:27
-1

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

dan1111
  • 6,576
  • 2
  • 18
  • 29