Python - Regular Expression


Regular Expressions



Regular expressions in Python can be accessed using the re module, which is part of the standard library. 



import re

pattern = r"spam"

if re.match(pattern, "spamspamspam"):
print("Match")
else:
print("No match")


-------------------

re.search finds a match of a pattern 

re.findall returns a list of all substrings that match a pattern.
group which returns the string matched,
start and end which return the start and ending positions of the first match
 span which returns the start and end positions of the first match as a tuple


Search & Replace

re.sub(pattern, repl, string, max=0)
-------------------

Thanks to sololearn.com

Share this

Related Posts

Previous
Next Post »