[英]Regex for $gme or gme with space or no space after or before it in python
I want a regex that can find all $gme or gme.我想要一个可以找到所有 $gme 或 gme 的正则表达式。 It should not find $gme or gme in a string like
它不应该在类似的字符串中找到 $gme 或 gme
hello $gmee
你好 $gmee
or或者
hello g$gme
你好g$gme
So for example in a string such as因此,例如在一个字符串中,例如
Hello gme.
你好。 Hey $gme.
嘿$gme。 Hey $gmee.
嘿$gmee。 Hey gmee.
嘿嘿。
It should find only gme or $gme from the above string.它应该只从上面的字符串中找到 gme 或 $gme。
It must also find gme or $gme when there is no space before or after a string For example当字符串前后没有空格时,它也必须找到 gme 或 $gme 例如
$gme
$gme
or或者
gme
游戏
I am writing a script that can count the number of times $gme or gme is mentioned in a string我正在编写一个脚本,可以计算字符串中提到 $gme 或 gme 的次数
So in a string like所以在一个字符串中
Hello gme.
你好。 Hey $gme.
嘿$gme。 Hey $gmee.
嘿$gmee。 Hey gmee.
嘿嘿。
Its mentioned only two times.它只提到了两次。
I hope its clear now.我希望它现在清楚了。
Regex I have tried我试过的正则表达式
\$gme$|\sgme\s|\sgme|^gme\S
See the demo screenshot .请参阅演示屏幕截图。
The above one fails when there is no space before and after $gme or gme.当 $gme 或 gme 前后没有空格时,上面的失败。 And also it includes gmee and $gmee which I don't want to include.
它还包括我不想包括的 gmee 和 $gmee。
You can use use a mix of whitespace and word boundaries:您可以混合使用空格和单词边界:
(?<!\S)\$?gme\b
See the regex demo查看正则表达式演示
Details :详情:
(?<!\S)
- a negative lookbehind that fails the match if there is no non-whitespace char immediately to the left of the current position (so, start of string or a whitespace must occur immediately on the left) (?<!\S)
- 如果当前 position 左侧没有非空白字符,则匹配失败的否定后向查找(因此,字符串的开头或空格必须立即出现在左侧)\$?
- an optional $
char $
chargme
- a word gme
- 一个词\b
- a word bounary. \b
- 一个词边界。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.