Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
First and last match with Regular Expressions

Conventional Wisdom says that it's pointless to start a regular expression with ".*" or ".+", as this is implied within a match - regular expression matches are looking for strings that are contained anyway:

'/abc/' - contains abc
'/.*abc/' - contains anything or nothing, followed by abc

However - conventional wisdom isn't the full story.

If you're just checking whether something matches, then - fair enough - the checks above are identical. But if you are using capture parentheses (round brackets to save interesting bits into new variables) then an extra .* DOES make a difference. Consider this piece of Perl:

$first = '/1234234/2234234/3423423/5234234/634234/';
 
($p1) = $first =~ m!(/\d+)!;
print ("$p1 \n");
 
($p1) = $first =~ m!.*(/\d+)!;
print ("$p1 \n");


Which runs as follows:

[trainee@holt lm10]$ perl dstar.pl
/1234234
/634234
[trainee@holt lm10]$


What is the difference between the two? The first has given the first possible match, and the second has given the last possible match - that's because the ".*" acts as a sponge and eats up as much of the incoming string as possible. If you're looking for a yes / no as to whether something matches, conventional wisdom of "no leading .*" is correct ...if you're looking for match strings, .* makes all the difference.

The first example I gave was PHP "Perl Style" regular expressions, the second was in Perl .. and this technique applies across other languages that use regular expressions. There's a modified behaviour with Python's match method as it only checks at the start of a string.

I wrote this technical briefing as a result of a question on a Lua course - and yet Lua does NOT support regular expressions - "Lua is a small language, and a regular expression engine would be bigger that our entire standard library - it is too expensive" is the reasoning. But it does have pattern matching, and that gives you almost everything you want - it looks very "regular expression like". See the 80 / 20 rule.

So here is the same application coded in Lua:

first = '/1234234/2234234/3423423/5234234/634234/'
 
p1 = string.match(first,'/%d+')
print (p1)
 
p1 = string.match(first,'.*(/%d+)')
print (p1)


There are Lua "patterns" and not regular expressions - and the techninques look - remarkably - similar!

We run Regular Expression workshops from time to time - see [here] for details. We also run courses in the other languages mentioned that include regular expression or pattern matching - see [here] for a course listing with onward links to individual descriptions

(written 2010-04-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
U108 - Lua - Pattern matching
  [1744] Lua examples, Lua Courses - (2008-08-08)
  [1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18)
  [2383] Lua Regular Expressions - (2009-08-28)
  [2727] Making a Lua program run more than 10 times faster - (2010-04-16)
  [3687] Binary / bitwise operations in Lua with the standard bit32 library - (2012-04-06)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)

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

P669 - Perl - Data Munging
  [597] Storing a regular expression in a perl variable - (2006-02-09)
  [1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
  [1509] Extracting information from a file of records - (2008-01-16)
  [1947] Perl substitute - the e modifier - (2008-12-16)
  [2129] Nothing beats Perl to solve a data manipulation requirement quickly - (2009-04-14)
  [3335] Practical Extraction and Reporting - (2011-06-26)
  [3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
  [3764] Shell, Awk, Perl of Python? - (2012-06-14)
  [4620] Perl 6 - a Practical Extraction and Reporting example! - (2016-01-11)


Back to
Is Lua an Object Oriented language?
Previous and next
or
Horse's mouth home
Forward to
Lua Metatables
Some other Articles
Error trapping in Lua - no exceptions.
Hotel booking in Melksham made easy!
A walk within without - Melksham Without
Lua Metatables
First and last match with Regular Expressions
Is Lua an Object Oriented language?
The same very simple program in many different programming languages
Lua tables - they are everything
Ruth Davis, 1916 - 2010
Email metrics and filtering
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., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2702_Fir ... sions.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb