Regular expressions with symbols

Previous
Next

Extract data using symbols:

  • We can create expressions using symbols.
  • To extract the data, we use symbols and operators in regular expressions.

Split the string into characters: Using dot(.) as a raw string, we can split into characters including spaces.

import re
line = "Python and Java"
arr = re.findall(r'.' , line)
print(arr)
print("Number of characters :", len(arr))
print("Count of 'a' :", arr.count('a'))

Excluding spaces: ‘\w’ represents a word in the input string

import re
line = "Python and Java"
arr = re.findall(r'\w' , line)
print(arr)
print("Number of characters :", len(arr))

Word split including spaces:

import re
line = "Python and Java"
arr = re.findall(r'\w*' , line)
print(arr)
print("Length is :", len(arr))

Word split excluding spaces:

import re
line = "Python and Java"
arr = re.findall(r'\w+' , line)
print(arr)
print("Number of words :", len(arr))

Extract the first word in input string.

import re
line = "Python and Java"
word = re.findall(r'^\w+' , line)
print("First word :", word)

Extract the first word in input string.

import re
line = "Python and Java"
word = re.findall(r'\w+$' , line)
print("Last word :", word)

Meta characters: A special meaning of Meta characters is called characters

Character     Description                                                               Example       

[]                      A set of characters                                                 “[a-m]”           

\                      Signals a special sequence                                        “\d”           

.                       Any character (except newline character)      “he..o”           

^                      Starts with                                                                 “^hello”         

$                      Ends with                                                                   “world$”        

*                      Zero or more occurrences                                         “aix*”  

+                      One or more occurrences                                         “aix+”

Previous
Next

Add Comment

Courses Enquiry Form