Understanding Python Regular Expressions

python regular expressions n.w
1 / 9
Embed
Share

"Explore how Python's re module provides powerful support for regular expressions, including pattern matching, substitution, flags, and more. Learn how to use match objects, pattern match flags, search and replace, split strings, and find all occurrences of a pattern in this comprehensive guide."

  • Python
  • Regular Expressions
  • Pattern Matching
  • Substitution
  • Flags

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Python: Regular Expressions

  2. re Module Provides support of regular expressions Very similar to Perl support of regular expression Character sets Anchors Modifiers Extended RE Pattern match Pattern substitution 2

  3. Pattern Match re.match(pattern, string, [flag]) Try to match pattern at beginning of string Return a match object if successful, None otherwise re.search(pattern, string, [flag]) Try to match pattern anywhere in the string Return a match object if successful, None otherwise str = hello, world m1 = re.match(r he , str) m2 = re.match(r (.)\1 , str) # match found # match not found m3 = re.search(r he , str) m4 = re.search(r (.)\1 , str) # match found # match found 3

  4. Match Object Method group([num]) group(0) also returns the entire match group(n) returns the nth parenthesized match subgroup group() returns all matched subgroups, same as group(0) str = hello, world m1 = re.search(r he , str) if (m1): print(m1.group(0)) m2 = re.search(r (.)\1 , str) If (m2): print(m2.group(0)) print(m2.group(1)) See examples/python/p5/example1.py 4

  5. Pattern Match Flags re.I Case-insensitive matching re.search(pattern, str, re.I) re.S Make period (.) match any characters, including newline re.X Ignore whitespaces and treats unescaped # as comment marker re.search( \w+ @ \w+ , str, re.X) Multiple flags can be provided with exclusive OR | re.search(pattern, string, re.S|re.X) 5

  6. Search and Replace re.sub(pattern, replacement, string, max=0) Search for pattern in string, and replace it with replacement whenever there is a match Max specifies the maximum number of replacement (replace all if unspecified) Return resulting string phone = 2004-959-559 # comments Num = re.sub(r #.*$ , , phone) Num = re.sub(r \D , , phone) See examples/python/p5/example2.py 6

  7. Split String using Regular Expression re.split(pattern, string, maxsplit=0, flags=0) Split string using pattern Returning a list of strings between pattern matches str = this is a test re.split(r \W+ , str) # list ['this', 'is', 'a', 'test'] See examples/python/p5/example3.py 7

  8. Find All Occurrence of Pattern re.findall(pattern, string, flag = 0) Return all non-overlapping matches of pattern in string str = this is a test re.findall(r \w+ , str) # list ['this', 'is', 'a', 'test'] # note \w 8

  9. Compiled Regular Expression re.compile(pattern, flags=0) Compile regular expression pattern into regular expression object, which can be used to call re methods prog = re.compile(r (.)\1 , re.I) prog.search(str) re.search(r (.)\1 , str, re.I) 9

More Related Content