Multiline in Python Regular Expressions
Regular expressions have their own peculiarities when working with multi-line strings. Let's look at them using the following string as an example, containing regular characters, line feed characters, and tabs:
txt = '''111
222
333
444'''
Line break
Newline characters can be caught with the '\n' command:
res = re.sub('\n', '!', txt)
print(res)
The result will be the following line (the spaces between lines are tabs):
'111! 222! 333! 444'
Tabulation
Tab characters can be caught with the '\t' command:
res = re.sub('\t', '!', txt)
print(res)
Result of code execution:
'''
!111
!222
!333
!444
'''
Work point
The '.' command for a multi-line string does not catch line feed characters:
res = re.sub('.', '!', txt)
print(res)
Result of code execution:
'''
!!!
!!!!
!!!!
!!!!
'''
Any symbol
To catch all characters in a multi-line string, a clever trick is used in the form of a combination of [\s\S]. This construction will find all regular characters and all line breaks:
res = re.sub('[\s\S]', '!', txt)
print(res)
Result of code execution:
'!!!!!!!!!!!!!!!!!!'
Dollar
The '$' command in multiline mode will catch the end of each line:
res = re.sub('$', '!', txt)
print(res)
Result of code execution:
'''
111!
222!
333!
444!
'''
Practical tasks
Given a string:
'''
abc
def
ghi
jkl
'''
Write a regular expression that will transform this line into the following:
'''
abc!
def!
ghi!
jkl!
'''
Given a string:
'''
abc
def
ghi
jkl
'''
Write a regular expression that will transform this line into the following:
'''
! abc
! def
! ghi
! jkl
'''
Given a string:
'''
abc
def
ghi
jkl
'''
Write a regular expression that will transform this line into the following:
'''!
abc
def
ghi
jkl
!'''
Given a string:
'''
abc
def
ghi
jkl
'''
Write a regular expression that will transform this line into the following:
'''!
! abc
! def
! ghi
! jkl
!'''
Given a string:
'''
abc
def
ghi
jkl
'''
Write a regular expression that will transform this line into the following:
'''!
abc!
def!
ghi!
jkl!
!'''
Given a string:
'''
abc
def
ghi
jkl
'''
Write a regular expression that will transform this line into the following:
'''
!abc
!def
!ghi
!jkl
'''