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 05:48:56)
Associated topics are indexed under
P212 - Perl - More on Character StringsQ803 - Object Orientation and General technical topics - Regular Expressions - Extra ElementsT205 - Tcl/Tk - String Handling in TclT247 - Tcl/Tk - Advanced Regular ExpressionsY108 - Python - String Handling
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