Regex for $gme or gme with space or no space after or before it in python

Moin

I want a regex that can find all $gme or gme. It should not find $gme or gme in a string like

hello $gmee

or

hello g$gme

So for example in a string such as

Hello gme. Hey $gme. Hey $gmee. Hey gmee.

It should find only gme or $gme from the above string.

It must also find gme or $gme when there is no space before or after a string For example

$gme

or

gme

I am writing a script that can count the number of times $gme or gme is mentioned in a string

So in a string like

Hello gme. Hey $gme. Hey $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. And also it includes gmee and $gmee which I don't want to include.

Wiktor Stribiżew

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)
  • \$? - an optional $ char
  • gme - a word
  • \b - a word bounary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Regex match space before or after characters

From Dev

Minimising AI usage in a gme

From Dev

Regex to extract only text after string and before space

From Dev

Regex to match string after last space and before a square bracket

From Dev

Python Regex Match Before Character AND Ignore White Space

From Dev

Add space before a substring in a string using regex in python

From Dev

Removing the space before and after the number

From Dev

How to make Python only look at commas with no space before or after as delimiters

From Dev

regex to exclude after first space

From Dev

Preserve space after delimeter regex

From Dev

Python regex : adding space after comma only if not followed by a number

From Dev

What is the Python Regex pattern to match only the first character after a space?

From Dev

Python regex : adding space after comma only if not followed by a number

From Dev

Regex leading space/add trailing space before/to punctuation

From Dev

matching space only before character regex and replacing space with dash

From Dev

vba regex - Get non space characters before space

From Dev

vba regex - Get non space characters before space

From Dev

RegEx: Only start if nothing or space is before string

From Dev

How to grab values before space and after a space in c++

From Dev

Python regex match space only

From Dev

Can someone explain how to design a Regex that works regardless whether there is a space or not after/before HTML tags

From Dev

Add space in the string before and after certain characters

From Dev

jQuery - Add space after and before comma

From Dev

Space(s) before/after the scope resolution operator

From Dev

Removing Space Before and After List of Equations in Latex

From Dev

Match everything after backslash and before space

From Dev

how to remove space before and after sub tag

From Dev

Removing Space Before and After List of Equations in Latex

From Dev

Removing unexpected space in :before :after pseudo elements

Related Related

  1. 1

    Java Regex match space before or after characters

  2. 2

    Minimising AI usage in a gme

  3. 3

    Regex to extract only text after string and before space

  4. 4

    Regex to match string after last space and before a square bracket

  5. 5

    Python Regex Match Before Character AND Ignore White Space

  6. 6

    Add space before a substring in a string using regex in python

  7. 7

    Removing the space before and after the number

  8. 8

    How to make Python only look at commas with no space before or after as delimiters

  9. 9

    regex to exclude after first space

  10. 10

    Preserve space after delimeter regex

  11. 11

    Python regex : adding space after comma only if not followed by a number

  12. 12

    What is the Python Regex pattern to match only the first character after a space?

  13. 13

    Python regex : adding space after comma only if not followed by a number

  14. 14

    Regex leading space/add trailing space before/to punctuation

  15. 15

    matching space only before character regex and replacing space with dash

  16. 16

    vba regex - Get non space characters before space

  17. 17

    vba regex - Get non space characters before space

  18. 18

    RegEx: Only start if nothing or space is before string

  19. 19

    How to grab values before space and after a space in c++

  20. 20

    Python regex match space only

  21. 21

    Can someone explain how to design a Regex that works regardless whether there is a space or not after/before HTML tags

  22. 22

    Add space in the string before and after certain characters

  23. 23

    jQuery - Add space after and before comma

  24. 24

    Space(s) before/after the scope resolution operator

  25. 25

    Removing Space Before and After List of Equations in Latex

  26. 26

    Match everything after backslash and before space

  27. 27

    how to remove space before and after sub tag

  28. 28

    Removing Space Before and After List of Equations in Latex

  29. 29

    Removing unexpected space in :before :after pseudo elements

HotTag

Archive