Skip to content

Instantly share code, notes, and snippets.

@PabloVallejo
Created June 18, 2013 02:20
Show Gist options
  • Save PabloVallejo/5802169 to your computer and use it in GitHub Desktop.
Save PabloVallejo/5802169 to your computer and use it in GitHub Desktop.
Python Regular expressions examples
import re
m = re.search('(?<=abc)def', 'abcdef')
# Getting first match
m.group(0) # -> 'def'
m = re.search('(?<=-)\w+', 'spam-egg')
m.group(0) # -> 'egg'
# Replace whitespace
s = re.sub( r'[\s]', '-', 'whitespace by hyphen' )
# Replce multiple occurences of whitespace
re.sub( r'\s{2,}', '', s )
# Add space after an period followed by a word
re.sub( r'(?<=\.)(?=[a-zA-Z])
# Find all words in string
re.findall( '\w+', s )
@Capriatto
Copy link

useful, thx

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