Strings 1
Strings 1
Strings 1
Strings
Regular expressions
Operations on strings
String transformations
◮ The special notations, like \Z, also cause problems with string
handling.
◮ Remember that normally in string literals we have some
special notation: \n stands for newline, \t stands for tab, and
so on.
◮ So, both string literals and regular expressions use similar
looking notations, which can create serious confusion.
◮ This can be solved by using the so-called raw strings. We
denote a raw string by having an r letter before the first
quotation mark, for example r’ab*\Z’
◮ When using raw strings, the newline (\n), tab (\t), and other
special string literal notations aren’t interpreted. One should
always use raw strings when defining regular expression
patterns.
Jarkko Toivonen (CS Department) Programming in Python 15
Strings Regular expressions
RE pattern syntax
Breaks
◮ The patterns \A, \b, \B, and \Z will all match an empty
string, but in specific places
◮ The patterns \A and \Z will recognise the beginning and end
of the string, respectively
◮ Note that the patterns ˆ and $ can in some cases match also
after a newline and before a newline, correspondingly.
◮ So, \A is distinct from ˆ, and \Z is distinct from $
◮ The pattern \b matches at the start or end of a word. The
pattern \B does the reverse.
Examples 1
Examples 2
Examples 3