Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Regular expressions made easy - building from components
There seems to be a certain macho desire in many programmer's minds to write a single complicated regular expression to match against an input line, ignorning the structured approach that everyone accepts quite cheerfully in almost every other case. Have a look at this Python line:

wholeline = r"\d\d-...-\d\d\d\d\s+(\d\d):(\d\d):(\d\d.\d\d),\s+(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+),(-?\d+\.\d+),\s+(-?\d+\.\d+)"

Impressive, isn't it?
Yes
Easy to follow, isn't it?
No!

Much better to build it up from a series of components:

date = r"\d\d-...-\d\d\d\d"
time = r"(\d\d):(\d\d):(\d\d.\d\d)"
whitespace = r"\s+"
floater = r"(-?\d+\.\d+)"
wholeline = date + whitespace + time + "," + whitespace + \
  floater + "," + whitespace + floater + "," + floater + \
  "," + whitespace + floater + "," + floater + "," + \
  whitespace + floater


These examples are from the Python Course I have just concluded - the full example is here - where a log file was to be analysed and a short report generated to highlight any changes in readings of over 1% from one line of the data to the next in any of the data columns.

(written 2007-08-16 20:25:39)

 
Associated topics are indexed under
Y115 - Additional Python Facilities
Q806 - Regular Expression Cookbook
R109 - Ruby - Strings and Regular Expressions
T247 - Tcl/Tk - Advanced Regular Expressions
P212 - Perl - More on Character Strings

Back to
Last elements in a Perl or Python list
Previous and next
or
Horse's mouth home
Forward to
Python class rattling around

Some other Articles
Dates for Easter - 2008 to 2015
Good to be home
Troy, up state New York
Python class rattling around
Regular expressions made easy - building from components
Last elements in a Perl or Python list
Heading Upstate New York
Ruby, Ruby, Ruby. Rails, Rails, Rails.
Plastic or China
In the USA for a few days
1638 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 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