如何用正则表达式匹配除@开头单词外的所有‘.’字符?
Got it, let's break this down—two hours of regex101 and forum deep dives can be brutal, so I’m glad you asked. You need to target every . in your string that isn’t part of a word starting with @, right?
Here’s the regex that’ll do exactly what you need:
(?<!@[^\s]*)\.
Let’s unpack why this works:
(?<!@[^\s]*): This is a negative lookbehind assertion. It checks that the.we’re about to match is NOT preceded by an@followed by any number of non-whitespace characters (aka the entire @-prefixed word, like@sys.anyor@names). If the.is inside one of those @-words, this assertion fails, so the regex skips it.\.: Escapes the dot character (since unescaped.matches any character in regex) to target only literal dots.
Testing this on your sample string:
this is @sys.any and. here @names hello. and good.bye
It will match exactly the dots you want:
- The dot in
and. - The dot in
hello. - The first dot in
good.bye
It will not match the dot in @sys.any because that’s part of an @-prefixed word.
A quick note: If you ever have @-words with multiple dots (like @user.first.last), this regex still works—it’ll ignore all dots inside those @-prefixed tokens automatically.
内容的提问来源于stack exchange,提问作者Daniel Netzer

