Ashton Ashton - 3 months ago 22
C# Question

Regex match parenthesis

I have the following string:

The service(s), foo bar


I have a regex that checks if the words 'The' and 'service(s)' are contained within the string. I'm supposed to escape the brackets with backslash, but it does not seem to work. I must use the logical structure of this Regex. Can you help me fix the escaping?

Not working:

(^(?=.*\b(?i)The(?-i)\b)(?=.*\b(?i)service\(s\)(?-i)\b).*$)


Target language is C#.

Answer

Closing parenthesis in service(s) asserts that next immediate position is a non-word boundary. So you don't need last \b token since you possibly meant a non-word boundary \B. I don't think you need to set and unset case-insensitive modifier either:

(?i)(?=.*\bThe\b)(?=.*\bservice\(s\)\B).*$