How to capture values with a space after hyphen with regex?

by darrion.kuhn , in category: Third Party Scripts , 2 months ago

How to capture values with a space after hyphen with regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 2 months ago

@darrion.kuhn 

To capture values with a space after a hyphen using regex, you can use the following regular expression pattern:

1
-s(w+)


Explanation of the pattern:

  • -s: This part of the pattern matches a hyphen followed by a space.
  • (w+): This part of the pattern uses a capturing group to capture one or more word characters after the hyphen and space.


Here is an example of how you can use this pattern in Python code to capture values with a space after a hyphen:

1
2
3
4
5
6
7
8
9
import re

text = "example - value"
pattern = r'-s(w+)'

matches = re.findall(pattern, text)

for match in matches:
    print(match)


In this example, the re.findall() function is used to search for all occurrences of the pattern in the text variable. The captured values are then printed out.