Syntax
|
Definition
|
Examples
|
re.search(regex,str)
|
return match object if found, else None
|
pika = re.search(r"\w+@\w+\.com", "from pikachu@pokemon.com address")
if pika :
print "yes"
print pika.group() # → pika@pika.com
else:
print "no"
|
re.match(regex,str)
|
similar to re.search(), but match starts at beginning
of string.
|
goku = re.match('ha','kamehameha') # succeed
if goku == None:
print "no match"
else:
print "yes match"
|
re.split(regex,str)
|
return a list.
|
print re.split(r' +', 'Clark Kent is Superman')'
# output: ['Clark', 'Kent', 'is', 'Superman']
print re.split(r'( +)(@+)', 'what @@do @@you @@think')
# output: ['what', ' ', '@@', 'do', ' ', '@@', 'you', ' ', '@@', 'think']
print re.split(r' ', 'a b c d e', maxsplit = 2)
# output: ['a', 'b', 'c d e']
|
re.findall(regex,str)
|
return a list of non-overlapping (repeated) matches.
|
print re.findall(r'( +)(@+)', 'what @@@do @@you @think')
# output: [(' ', '@@@'), (' ', '@@'), (' ', '@')]
|
re.finditer(…)
|
similar to re.findall(), but returns a iterator.
|
for matched in re.finditer(r'(\w+)', 'where are the avengers'):
print matched.group() # prints each word in a line
|
re.sub(regex,replacement,str)
|
does replacement. Returns the new string.
|
def ff(pika):
if pika.group(0) == "bulba":
return "vena"
elif pika.group(0) == "mender":
return "izard"
else:
return pika.group(0)
print re.sub(r"[aeiou]+", ff, "bulbasaur") # venasaur
print re.sub(r"[aeiou]+", ff, "charmender") # charizard
print re.sub(r"[aeiou]+", ff, "geek") # geek
|
re.subn(…)
|
similar to re.sub(), but returns a tuple. 1st element
is the new string, 2nd is number of replacement.
|
re.subn('\w+', 'Try', 'Cry Cry, Cry and Cry Again!', count=3)
# ('Try Try, Try and Try Again!', 3)
|
re.escape(str)
|
add backslash to string for feeding it to regex as pattern.
Return the new string.
|
re.escape('Lets meet spider man?')
# output: Lets\\ meet\\ spider\\ man\\?
|
View comments