Regex help.
For Learning purposes
I want to match
C#
in given string. But the below pattern doesn't work for C#
while it works for any alphabet word.
7 Replies
.NET regexes don't use
?i
IIRC? There are flags on Regex for that instead
Ah no it does work, huh. It's unusual
Ah, it's because "#" doesn't count as the end of a wordhttps://learn.microsoft.com/en-us/dotnet/standard/base-types/anchors-in-regular-expressions#word-boundary-b
https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-classes-in-regular-expressions#word-character-w
Anchors in .NET Regular Expressions - .NET
Learn how to use anchors in regular expression patterns.
Character Classes in .NET Regular Expressions - .NET
Learn how to use character classes to represent a set of characters in .NET regular expressions.
"#" is in the "Other Punctuation (Po)" category
(^|\W)(C#)(\W|$)
or something might do it?I don't know what you are trying to achieve given that this question is for fixing your regex, but if the whole point is to determine the location of "C#" in your string, then please just use IndexOf instead of overcomplicating it with a regex.
Thank you, I was wondering for hours lol. Ended up using
(?i:\bC#\B)
. Just replaced the last \b
with \B
since #
is not a word character.Hah, fair
Its for learning purposes only. Thank you for suggestion.