Python Regular Expression or Regex Tutorial With Examples


182 Views | Read Time 3 minutes |

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.RegEx can be used to check if a string contains the specified search pattern.

  • Regular Expression are symbols in python that make your job in extracting much easy
  •  Before you can use regular expressions in your program, you must import the library using “ import re
  • There are some functions that you can use by this library that makes extracting data easier to code
  1. re.search()
    • To see if a string matches a regular expression, similar to the find() method for strings
  2. re.findall()
    • To see if a string matches a regular expression, similar to “find()+slicing

Regular Expressions in python:

  1. ( ^ )               Matches the beginning of a line
  2. ( . )               Matches any character
  3. ( $ )               Matches The End of a line
  4. ( s )             Matches any whitespace character
  5. ( S )             Matches any non-whitespace character
  6. ( * )               Repeats a character zero or more time
  7. ( *? )             Repeats a character zero or more time (Non greedy)
  8. ( + )               Repeats a character one or more time
  9. ( +? )             Repeats a character one or more time (Non greedy)
  10.  [aeiou]         Matches a single character in the listed set
  11.  [^XYZ]          Matches a single character, not in the listed set
  12.  [a-z0-9]        the set of character can include a range
  13.  (                   indicates where string extracting is to start
  14.  )                   indicates where string extracting is to End
  15.  [  ]  nonblank character

Examples

In this example, we will give a simple code to find any emails in a text file

Step 1:

  • When you use a regular expression you should import its library first which called ‘re’ that library gives you all the regular expressions that you need to use in the program.
  • You can import this library by writing at the beginning of the code (import re)
READ ALSO  What you need to know about Peloton's warning about internal controls flaws

Step 2:

  • Make an input function for the user to let him
    enter the name of the text file
  • Also, add to the name the text file extension (.txt)

Step 3:

  • You should use open function now to open the file and read it, using open() and an attribute ‘r’

Step 4:

  • Now we should start dealing with each line separately, so you should make a for loop
  • inside that loop, you should add a statement block to find each line that has an email
  • if you were about to use anything else as the previous tutorials, it would take many lines from you to find an email inside that line but using regular expression it only needs two lines
  • you must use re.findall() and a regular expression ( ‘S+@S+’), that regular expression means you have to find any word that has ‘@’ and before and after the ‘@’ any word with no white spaces

Step 5:

  • Run the code to see how the regular expression code find the emails through the file

In this tutorial, We tried to understand the basics of Regular expressions in Python. Let us know about your thoughts or any concern in the below comments section.



Source link

?
WP Twitter Auto Publish Powered By : XYZScripts.com