Skip to content

Instantly share code, notes, and snippets.

@ansaso
Last active August 19, 2020 11:37
Show Gist options
  • Save ansaso/86005f87afb4057f6d957f8281130d29 to your computer and use it in GitHub Desktop.
Save ansaso/86005f87afb4057f6d957f8281130d29 to your computer and use it in GitHub Desktop.
useful regex
class usefulRegex:
def toggle(kwrd: str, match: str) -> str:
return r'(?:\W|\A)' + re.escape(kwrd) + r'[=: \'\"]+(' + re.escape(match) + r')'
def keyword(kwrd: str) -> str:
return r'(?:\W|\A)' + re.escape(kwrd) + r'[=: \'\"]+(?:\'(.+)\'|(\".+\")|(\S+))'
def value(kwrd: str, match: str=r'([\d.]+)') -> str:
return r'(?:\A|\W)' + re.escape(kwrd) + r'[ =:]*?' + re.escape(match)
def between(start: str, stop: str):
start = re.escape(start)
stop = re.escape(stop)
return r'(?:\A|\s)(?:' + start + r'){1}(?![' + start + r'])(.+?)(?<![' + stop + r'])(?:' + stop + r'){1}'
def split_list():
return r'\A[ \'\"]|[ \'\"]*,[ \'\"]*|[ \'\"]\Z'
def start_includes(match: str):
return r'^(?:(?:\<.+\>|&nbsp;)|\W)+' + re.escape(match)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment