473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Regex question: Lazy quantifiers

What is the point of lazy * and lazy ? ? "Nothing" will always succeed
first, right? If not, can someone give me an example of when either of these
might be used?
Jul 21 '05 #1
3 2296
In regular expressions, the (*) operator (star) is a cardinalty operator,
meaning "match zero or more occurences of the previous pattern". The (?)
operator is a cardinality operator for zero or one pattern match, of the
previous pattern.

so if you had:

(^abc*$) then you would match things like "abc", "abcc", "ab", "abcccccccc"

if you had:

(^abc?$) then you would match things like "ab", "abc"

then if you had:

(^ab*c?$) you would match "abc", "ac", "a", "abbbbbbbbc"

It's easy to confuse (*) and (?) with the globbing rules, which match ANY
STRING for (*) and ANY CHARACTER for (?). Regular expressions and globbing
are somewhat orthagonal in this case.

Hope that helps

-- Jake

"Jeff Johnson [MVP: VB]" wrote:
What is the point of lazy * and lazy ? ? "Nothing" will always succeed
first, right? If not, can someone give me an example of when either of these
might be used?

Jul 21 '05 #2
"Jeff Johnson [MVP: VB]" <i.***@enough.spam> wrote in
news:OR****************@TK2MSFTNGP10.phx.gbl...
What is the point of lazy * and lazy ? ? "Nothing" will always succeed
first, right? If not, can someone give me an example of when either of
these might be used?


I'm not sure if I got your question right, you want to know when/why to use
lazy quntifiers (*? or ??), is this correct?

You are correct, "nothing" will always succeed first, but if the subpattern
after the lazy quantifier doesn't match, the regex engine will try "more
than nothing". A common example is this one: pattern: "<div>.*</div>". If
the input sequence is:
<div>Line 1</div><div>Line 2</div><div>Line 3</div>
the pattern will match the whole text, becaue ".*" matches as much as
possible. (You might try this in Expresso or Regulator or something alike).
Using "<div>.*?</div>" will result in 3 matches, one for each "div".
Doing the same thing without lazy quantifiers is quite complex. (something
like "<div>([^<]|<[^d]|<d[^i]|<di[^v]|<div[^>\s])*</div>").

Hope this helps,

Niki
Jul 21 '05 #3

"Niki Estner" <ni*********@cube.net> wrote in message
news:e9**************@TK2MSFTNGP15.phx.gbl...
You are correct, "nothing" will always succeed first, but if the
subpattern after the lazy quantifier doesn't match, the regex engine will
try "more than nothing". A common example is this one: pattern:
"<div>.*</div>". If the input sequence is:
<div>Line 1</div><div>Line 2</div><div>Line 3</div>
the pattern will match the whole text, becaue ".*" matches as much as
possible. (You might try this in Expresso or Regulator or something
alike). Using "<div>.*?</div>" will result in 3 matches, one for each
"div".
Doing the same thing without lazy quantifiers is quite complex. (something
like "<div>([^<]|<[^d]|<d[^i]|<di[^v]|<div[^>\s])*</div>").


Excellent! That's exactly what I needed to know. It tells me that *? and ??
are only useful if they are followed in the regular expression by something
else.
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
2
by: Matt T. | last post by:
I am trying to replace the nested table tag in the follow string using a regular expression, but I am not having any success. I am new at using regular expressions, so I am sure I am just...
3
by: Jeff Johnson [MVP: VB] | last post by:
What is the point of lazy * and lazy ? ? "Nothing" will always succeed first, right? If not, can someone give me an example of when either of these might be used?
7
by: Bert | last post by:
hi How do I make a regex to find a word that starts en ends with a certain letter. What is in between is random. thanks b
3
by: MaxMax | last post by:
Is it possible to modify an expression with quantifiers in this way: ABCD to A!B!C!D! the pattern of the first expression would be something like: * but if I use this: But I don't know...
14
by: ohmmega | last post by:
hy, i've got a simple question (for somebody who already knows the answer) about regex: i've a string like bla@bla@bla or bla@@bla i like to check the @'s, but couldn't figure it out how to set...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
1
by: dbdariusz | last post by:
Hi input: <!-- comment -- <script><!-- comment --</scritp <!-- comment --> output: <script><!-- comment --></scritp>
4
by: Danny Ni | last post by:
Hi, The following code snippet is causing CPU to max out on my local machine and production servers. It looks fine on Expresso though. Regex rgxVideo = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.