Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Commenting a Perl Regular Expression

The x modifier on the end of a Perl regular expression causes all spaces in the regular expression to be treated as comments (rather than matching exactly). This means that you can lay out your regular expressions much more cleanly.

And wherever you're allowed white space, you can add comments from # to end of line. So the following code (for finding Belgian car registrations in a line of input) is valid:

while ($info = <>) {
        while ($info =~ /
                \b
                ([A-Z]{3})      # capture letters to $1
                -?
                (\d{3})         # capture digits to $2
                \b
                /gx             ) {
           print "Yes, $2, $1\n";
        }
        print "And that's your lot\n";
}


Note that you should NOT use the regular expression terminator (/ in our example) within the comments you add ... and if you REALLY want to match a space, you should now use \s for any space character or \x20 for a single space.
(written 2007-06-12 23:37:33)

 
Associated topics are indexed under
P212 - Perl - More on Character Strings
Q806 - Regular Expression Cookbook

Back to
Where am I?
Previous and next
or
Horse's mouth home
Forward to
Sur le Continent
Some other Articles
A review of a week and a trip to Brugge
Horses of Brugge
Bathtub example
Sur le Continent
Commenting a Perl Regular Expression
Where am I?
Some progress on the train campaign
Melksham businesses - networking together
Melksham Art Cafe
Perl - functions for directory handling
1819 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho