lunes, 15 de junio de 2015

Regular expressions

"""
Regular expressions
^ Matches the beginning of a line
$ Matches the of the line
. Matches any character
\s Matches whitespace
* Repeats a character zero or more times
*? Repeats a character zero or more times (non-greedy)
+ Repeats a character one or more times
+? Repeats a character one or more times (non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
to use regular expressions
import re

-------------wild-card character--------
"""
import re
x = 'my 2 favorite numbers are 19 and 42'
# find one or more digits
y = re.findall('[0-9]+', x)
print y

Usifull links