Reputation: 55
lets assume i have a word like "aus" and i want to replace all occurences of it with "off". Then i also want to find all possible spellings of it like "AUs", "aUs", "AUS" and so forth. But its important that its only replaced when it "stands alone" as in has only a space, an underscore (_) or nothing in front and/or after it so it should be replaced in
" aus"
"aus"
"_aus"
"_aus_"
"aus_"
But not in
"ausschalten"
"aushebeln"
" ausschalten"
I tried ^(_| )(A|a)(U|u)(S|s)(_|)$
but its not working right :/
Upvotes: 1
Views: 107
Reputation: 627327
You can make use of lookarounds and a RegexOptions.IgnoreCase
flag (or its inline version (?i)
):
@"(?i)(?<![\w-[_]])aus(?![\w-[_]])"
See regex demo
Explanation:
(?<![\w-[_]])
- check if before aus
there is no digit or letter character (using character class subtraction, I removed _
from \w
class)aus
- literal character sequence aus
(?![\w-[_]])
- check if after aus
there is no letter or digit.A simpler alternative with \p{L}
(any Unicode base letter) and \p{N}
(any digit):
(?i)(?<![\p{L}\p{N}])aus(?![\p{L}\p{N}])
Upvotes: 1