Differentiate Regex patterns and Regex modifiers. Explain any 3 uses of regular expression with relevant example.

  • regular expression in python is A tiny, highly specialized programming language embedded inside and made available through the re module.
  • it helps to match or find other strings or set of strings, using a specialised syntax held in a pattern.
  • The re module raises the exception re.error if an error occurs while compiling or using a regular expression.
  • There are various characters, which have special meaning when they are used in regular expression.
  • While using REs, we use raw string as r'expression'

Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these −

Sr.No.Modifier & Description
1re.I
Performs case-insensitive matching.
2re.L
Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior(\b and \B).
3re.M
Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string).
4re.S
Makes a period (dot) match any character, including a newline.
5re.U
Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
6re.X
Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.

Example

txt = "The rain in Spain"
x = re.findall(r"spain", txt,re.I)
print(x)

# Program to remove all whitespaces
import re

# multiline string
string = 'abc 12\
de 23 \n f45 6'

# matches all whitespace characters
pattern = '\s+'

# empty string
replace = ''

new_string = re.sub(pattern, replace, string) 
print(new_string)

# Output: abc12de23f456
import re

string = "Python is fun"

# check if 'Python' is at the beginning
match = re.search('\APython', string)

if match:
  print("pattern found inside the string")
else:
  print("pattern not found")  

# Output: pattern found inside the string

#replace new line,carrage return to tab 
import re

string = '\n and \r are escape sequences.'

result = re.sub(r'[\n\r]',r'\t' ,string)
print(result)

# Output: ['\n', '\r']

Leave a reply