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
(written 2006-11-25, updated 2006-11-26)
Associated topics are indexed under
P212 - Perl - More on Character Strings [3927] First match or all matches? Perl Regular Expressions - (2012-11-19)
[3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
[3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
[3630] Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
[3546] The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
[3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
[3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
[3322] How much has Perl (and other languages) changed? - (2011-06-10)
[3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[2877] Further more advanced Perl examples - (2010-07-19)
[2874] Unpacking a Perl string into a list - (2010-07-16)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[2801] Binary data handling with unpack in Perl - (2010-06-10)
[2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
[2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
[2230] Running a piece of code is like drinking a pint of beer - (2009-06-11)
[1947] Perl substitute - the e modifier - (2008-12-16)
[1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[1510] Handling Binary data (.gif file example) in Perl - (2008-01-17)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1251] Substitute operator / modifiers in Perl - (2007-06-28)
[1230] Commenting a Perl Regular Expression - (2007-06-12)
[1222] Perl, the substitute operator s - (2007-06-08)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[737] Coloured text in a terminal from Perl - (2006-05-29)
[608] Don't expose your regular expressions - (2006-02-15)
[597] Storing a regular expression in a perl variable - (2006-02-09)
[586] Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
[583] Remember to process blank lines - (2006-01-31)
[453] Commenting Perl regular expressions - (2005-09-30)
Q803 - Object Orientation and General technical topics - Regular Expressions - Extra Elements [3516] Regular Expression modifiers in PHP - summary table - (2011-11-12)
[3089] Python regular expressions - repeating, splitting, lookahead and lookbehind - (2010-12-17)
[2909] Be gentle rather than macho ... regular expression techniques - (2010-08-08)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1613] Regular expression for 6 digits OR 25 digits - (2008-04-16)
[1601] Replacing the last comma with an and - (2008-04-04)
[1372] A taster PHP expression ... - (2007-09-30)
T205 - Tcl/Tk - String Handling in Tcl [3576] Tcl - apparently odd behaviour of string trimleft - (2012-01-13)
[3285] Extracting data from a string / line from file - Tcl - (2011-05-10)
[3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
[2472] split and join in tcl and expect - (2009-10-23)
[1410] Tcl / regsub - changing a string and using interesting bits - (2007-10-27)
[1403] Square Bracket protection in Tcl - (2007-10-23)
[779] The fragility of pancakes - and better structures - (2006-06-26)
[404] How to check that a string contains a number in Tcl - (2005-08-06)
T247 - Tcl/Tk - Advanced Regular Expressions [1412] Sparse and Greedy matching - Tcl 8.4 - (2007-10-27)
[1195] Regular Express Primer - (2007-05-20)
Y108 - Python - String Handling [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[3796] Backquote, backtic, str and repr in Python - conversion object to string - (2012-07-05)
[3469] Teaching dilemma - old tricks and techniques, or recent enhancements? - (2011-10-08)
[3468] Python string formatting - the move from % to str.format - (2011-10-08)
[3349] Formatting output in Python through str.format - (2011-07-07)
[3218] Matching a license plate or product code - Regular Expressions - (2011-03-28)
[3090] Matching to a string - what if it matches in many possible ways? - (2010-12-17)
[2814] Python - splitting and joining strings - (2010-06-16)
[2780] Formatted Printing in Python - (2010-05-25)
[2765] Running operating system commands from your Python program - (2010-05-14)
[2721] Regular Expressions in Python - (2010-04-14)
[2692] Flexible search and replace in Python - (2010-03-25)
[2406] Pound Sign in Python Program - (2009-09-15)
[2284] Strings as collections in Python - (2009-07-12)
[1876] Python Regular Expressions - (2008-11-08)
[1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
[1517] Python - formatting objects - (2008-01-24)
[1110] Python - two different splits - (2007-03-15)
[970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[954] Splitting Pythons in Bradford - (2006-11-29)
[903] Pieces of Python - (2006-10-23)
[773] Breaking bread - (2006-06-22)
[560] The fencepost problem - (2006-01-10)
[496] Python printf - (2005-11-15)
[463] Splitting the difference - (2005-10-13)
[324] The backtick operator in Python and Perl - (2005-05-25)
4ad9
Some other Articles
What is an SQL injection attack?Look around this mouth.Code quality countsJust ******* Google itMatching within multiline strings, and ignoring case in regular expressionsIndex of PicturesSnaggingWinter at Well House Manor - Open HousesSwipe cards for hotel rooms - Security issuesBratton and Edington new town, Wiltshire