Created
June 18, 2013 02:20
-
-
Save PabloVallejo/5802169 to your computer and use it in GitHub Desktop.
Python Regular expressions examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
useful, thx