Main Content
Matching within multiline strings, and ignoring case in regular expressions Archive - Originally posted on "The Horse's Mouth" - 2006-11-25 05:48:56 - Graham Ellis
Regular Expressions are powerful matching tools and you can specify almost anything within them. But there are certain facilities that are naturally applied to the regular expression as a whole rather than to parts of the match, and there are specified in a different way in each language / implementation.
For example, in what is commonly known as multiline mode you may want to match not only at the start / end of the string as a whole , but also match at embedded new lines . You can specify multiline mode as follows:
In Tcl, using the -lineanchor option
In Perl, with the /m modifier on the end of your regex
In Python by adding re.M or re.MULTILINE to your compile
Here's an example, in Tcl, looking for embedded lined containing just ABC:
set samples [list "Hello world\nABC\nThis matches" \
"Another test\nABCD\nNo match" ]
foreach sample $samples {
puts [regexp -lineanchor {^ABC$} $sample]
}
ther facilities often added onto your regular expression as modifiers include:
a) The ability to have "." (the dot) match any character at all , and not to exclude the newline character which it does by default. Sometimes known as single line of linestop mode.
In Tcl, leave off the -linestop option
In Perl, add /s
In python, add re.DOTALL onto the compile
b) The ability to ignore case in the match
In Perl, /i
In Python, re.I or re.IGNORECASE
In Tcl, use (?i through ) in the regex
c) The ability to add white space as comments into your expression
In Perl, /x
In Python, re.VERBOSE
In Tcl, use (?X through ) in the regex
Some other articles
Y108 - String Handling Prining a pound sign from Python AND running from the command line at the same time Python formatting update - including named completions Command line parameter handling in Python via the argparse module Python - comparison of old and new string formatters Identifying and clearing denial of service attacks on your Apache server Formatting options in Python Why are bus fares so high? Collections in Python - list tuple dict and string. Formatting output - why we need to, and first Python example Backquote, backtic, str and repr in Python - conversion object to string Teaching dilemma - old tricks and techniques, or recent enhancements? Python string formatting - the move from % to str.format Formatting output in Python through str.format Matching a license plate or product code - Regular Expressions Matching to a string - what if it matches in many possible ways? Python - splitting and joining strings Formatted Printing in Python Running operating system commands from your Python program Regular Expressions in Python Flexible search and replace in Python Pound Sign in Python Program Strings as collections in Python Python Regular Expressions Underlining in Perl and Python - the x and * operator in use Python - formatting objects Regular Express Primer Python - two different splits String duplication - x in Perl, * in Python and Ruby Splitting Pythons in Bradford This article Pieces of Python Breaking bread The fencepost problem Python printf Splitting the difference The backtick operator in Python and Perl T247 - Advanced Regular Expressions Regular Expression Substitution - Tcl Regular expression for 6 digits OR 25 digits Sparse and Greedy matching - Tcl 8.4 Tcl / regsub - changing a string and using interesting bits Ignore case in Regular Expression Regular expressions made easy - building from components Regular Express Primer This article T205 - String Handling in Tcl Working out distance between places, using OS grid references and a program in Tcl Everything is a string - even a list Regular Expression Substitution - Tcl Tcl - apparently odd behaviour of string trimleft Extracting data from a string / line from file - Tcl Tcl - Some example of HOW TO in handling data files and formats split and join in tcl and expect Replacing the last comma with an and Tcl / regsub - changing a string and using interesting bits Square Bracket protection in Tcl This article The fragility of pancakes - and better structures How to check that a string contains a number in Tcl Q803 - Regular Expressions - Extra Elements Possessive Regular Expression Matching - Perl, Objective C and some other languages Regular Expression modifiers in PHP - summary table Looking ahead and behind in Regular Expressions - double matching Python regular expressions - repeating, splitting, lookahead and lookbehind Be gentle rather than macho ... regular expression techniques Seven new intermediate Perl examples Finding words and work boundaries (MySQL, Perl, PHP) Regular expression for 6 digits OR 25 digits Replacing the last comma with an and A taster PHP expression ... Ignore case in Regular Expression This article P212 - More on Character Strings Binary data handling - Python and Perl First match or all matches? Perl Regular Expressions Converting codons via Amino Acids to Proteins in Perl Possessive Regular Expression Matching - Perl, Objective C and some other languages Serialsing and unserialising data for storage and transfer in Perl The difference between dot (a.k.a. full stop, period) and comma in Perl Single and double quotes strings in Perl - what is the difference? DNA to Amino Acid - a sample Perl script How much has Perl (and other languages) changed? Looking ahead and behind in Regular Expressions - double matching Object Orientation in an hour and other Perl Lectures Arrays v Lists - what is the difference, why use one or the other Further more advanced Perl examples Unpacking a Perl string into a list Teaching examples in Perl - third and final part Binary data handling with unpack in Perl Want to do a big batch edit? Nothing beats Perl! Making variables persistant, pretending a database is a variable and other Perl tricks Running a piece of code is like drinking a pint of beer Perl substitute - the e modifier Finding words and work boundaries (MySQL, Perl, PHP) Equality and looks like tests - Perl Handling Binary data (.gif file example) in Perl Ignore case in Regular Expression Regular expressions made easy - building from components Substitute operator / modifiers in Perl Commenting a Perl Regular Expression Perl, the substitute operator s This article C++ and Perl - why did they do it THAT way? Coloured text in a terminal from Perl Don't expose your regular expressions Storing a regular expression in a perl variable Perl Regular Expressions - finding the position and length of the match Remember to process blank lines Commenting Perl regular expressions