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)
Some other Articles
Error trapping in Lua - no exceptions.Hotel booking in Melksham made easy!A walk within without - Melksham WithoutLua MetatablesFirst and last match with Regular ExpressionsIs Lua an Object Oriented language?The same very simple program in many different programming languagesLua tables - they are everythingRuth Davis, 1916 - 2010Email metrics and filtering