Nerd Tutorials

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

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
Performs case-insensitive matching.
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).
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).
Makes a period (dot) match any character, including a newline.
Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
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.


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

# 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) 

# Output: abc12de23f456
import re

string = "Python is fun"

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

if match:
  print("pattern found inside the string")
  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)

# Output: ['\n', '\r']
Exit mobile version