Skip to content

Instantly share code, notes, and snippets.

@tngTUDOR
Last active January 25, 2024 18:43
Show Gist options
  • Save tngTUDOR/05498bd3366248a4adaba4e057c66bc3 to your computer and use it in GitHub Desktop.
Save tngTUDOR/05498bd3366248a4adaba4e057c66bc3 to your computer and use it in GitHub Desktop.
python regex to capture SP names
# A regex to describe a (1)first group, (2) a space, (3)a location enclosed by curly braces
# (4)a bar and a space, (5)a second group, (6)a space, (7) a bar and a space, a (8)last group.
# Spaces are groups by themselves to ease further treatment
pattern = "([^|]+)( )(\\{[\\W\\w\\s]+\\})(\\| )([\\W\\w\\s]+)( )(\\| )([\\W\\w\\s]+)"
@tngTUDOR
Copy link
Author

tngTUDOR commented Jan 25, 2024

Getting the name, type and location could be done as follows:

import re
pattern = "([^|]+)( )(\\{[\\W\\w\\s]+\\})(\\| )([\\W\\w\\s]+)( )(\\| )([\\W\\w\\s]+)"

regex = re.compile(pattern)
match = regex.match("Potash - Potash salt {RER}| market for potash salt | Cut-off, S")
print(f"Name: {match.group(1)}")
print(f"Location: {match.group(3)}")
print(f"Type: {match.group(5)}")
print(f"System: {match.group(8)}")
Name: Potash - Potash salt
Location: {RER}
Type: market for potash salt
System: Cut-off, S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment