Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Regular Expression Myths

Does this look good to you?
  /.*\S+[@]\S+.*/

It shows some regular expression myths that I would like to explode!

Myth 1. If you want to match a specific character, you must put it in square brackets.

WRONG ... Square Brackets are a grouping - if you're looking to match just a single specific character, you can simply add it in without the square brackets. A word of caution ... there are a few characters which need \ protection to make sure they are taken literally outside []s, but which have no special significance within the []s.

Myth 2. If you want to match something in the middle of a string, you should start and end your regular expression with ".*" - i.e. anything, then (pattern), then anything.

WRONG ... regular expressions match within a string, so the .* on the beginning and the end are redundant. Two exceptions, however ... (i) - in Python, the match method looks at the beginning of a string, so if you're using it to look in the middle of a string, you'll need the .* and (ii) If you are capturing the string that matches - using capture parenthises for example - a leading .* will select a different match for you - it'll select the last match in your incoming string rather than the first match.

Myth 3. A "." matches any character at all.

WRONG ... by default, a "." does NOT match a new line character. This only makes a difference if you're matching against a string that may contain multiple lines of text, and this very slight restriction is applied by default so that you can safely match within a single record using .* even if you have multiple records in a long string. "Single line mode" - an s modifier in Perl, and re.DOTALL in Python, allow you to force a dot to truly match on any character including a new line!

Learning about Regular expressionsWe cover regular expressions on almost all of our courses [Schedule]. That's Perl, PHP, Python, Tcl, Ruby, ... they're also used and briefly covered on MySQL, Apache httpd (Linux Web Server, and Deploying LAMP), and we have a separate One day regular expression course too which is suitable for skilled programmers in any of the areas I have mentioned who wish to take their regular expressions further. Regular expression engines are available also in C and Java ... though we only cover them by request during courses on the subjects. Lua's pattern matching is very similar to Regular Expressions (and you can learn a lot from one about the other), but we do not mix the training - if you want to learn about Lua patterns, come on a Lua Course.


Illustration - course delegates. This article was inspired by the gentleman on the left of the picture, who had significant data to comb through and with whom I had long, fascinating and wide ranging discussions on regular expressions.
(written 2010-06-13, updated 2010-06-18)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q802 - Object Orientation and General technical topics - Regular Expression Elements
  [4763] Regex Reference sheet - (2017-10-10)
  [4505] Regular Expressions for the petrified - in Ruby - (2015-06-03)
  [1849] String matching in Perl with Regular Expressions - (2008-10-20)
  [1799] Regular Expressions in PHP - (2008-09-16)
  [1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
  [1480] Next course - 7th January 2008, Regular Expressions - (2007-12-21)
  [453] Commenting Perl regular expressions - (2005-09-30)

Q806 - Regular Expression Cookbook
  [3788] Getting more than a yes / no answer from a regular expression pattern match - (2012-06-30)
  [3218] Matching a license plate or product code - Regular Expressions - (2011-03-28)
  [2727] Making a Lua program run more than 10 times faster - (2010-04-16)
  [2702] First and last match with Regular Expressions - (2010-04-02)
  [2608] Search and replace in Ruby - Ruby Regular Expressions - (2010-01-31)
  [2563] Efficient debugging of regular expressions - (2010-01-04)
  [2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1305] Regular expressions made easy - building from components - (2007-08-16)
  [1230] Commenting a Perl Regular Expression - (2007-06-12)
  [672] Keeping your regular expressions simple - (2006-04-05)


Back to
Travelling across Europe
Previous and next
or
Horse's mouth home
Forward to
How are you getting on?
Some other Articles
From home to Nurnberg - journey pictures
Canal through Melksham - the options and issues
Macho matching - do not do it!
How are you getting on?
Regular Expression Myths
Travelling across Europe
After the Perl course in Nurnberg
Binary data handling with unpack in Perl
The Merchants And
Nuremberg - some pictures
4759 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, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2804_Reg ... Myths.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat