-1

I am trying to match a substring of "Number" or "Number(s)" in a string using a single Regex. However, I can get them to match individually, but not together.

Individually,
'Number' can match the word number

'Number[(]s[)]' can match Number(s).

However, if I put them together and do "Number|Number[(]s[)]" it is not matching for (s) of "Number(s)".

What I have tried:

1: Put \b boundary around the second string, doesn't work.

2: Use \ to escape, but C# yells at me for unrecognized escape sequence, so I opted out of this option

I know that I can use two regex to do what I want, but I wanted to understand what is wrong here and learn.

AngryOtter
  • 149
  • 9

1 Answers1

-1

Number|Number[(]s[)] wont match Number(s) because it's first part "Number" matches it. Try change the pattern part order: Number[(]s[)]|Number. This will try to match first with the string with parentheses and if it can't it will try the short form. Also the pattern should be: Number\(s\)|Number The unrecognized escape error message comes because if you want this pattern written as a string literal you must escape the backslash signs: "Number\\(s\\)|Number".

cly
  • 661
  • 3
  • 13
  • 1
    It would be very nice to have some explanations for -1! The answer was tested before posting and it was working! – cly Oct 10 '21 at 15:56