Solved

Regular Expressions - Why does this match?

Posted on 2010-08-26
5
230 Views
Last Modified: 2012-05-10
Hi All,

I am trying to get my head around RegEx, but I don't understand why this expression:

.*la\+*.*

matches this string (without the quotes):

"Alan"

I *thought* that expression says something like:

Match any string with "la+" inside it.

However, clearly it is more general than that?

Thanks,

Alan.
0
Comment
Question by:Alan3285
5 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33529855
it will match because

segregating .*la\+*.*

.* will match a in alan
la will la in alan
\+* will not match anything since it says '+' can come 0 or more times
.* will match n in alan

hope this helps
0
 
LVL 48

Expert Comment

by:hernst42
ID: 33529883
.*la\++.*

wozuld not match alan, but "la+" or 'foola+bar'
0
 
LVL 12

Author Closing Comment

by:Alan3285
ID: 33529900
It all seems so completely obvious when you explain it!

Thanks,

Alan.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33529903
thanks for the points :)
0
 

Expert Comment

by:AlJames
ID: 33529976
It matches as you have a wildcard in front and appending your regex.

you have basically stated:

Match any character [.]
any number of times [*]
followed by la           [la]
Zero or more matches [\+]
any number of times [*]
any character           [.]
any number of times [*]

so that regex pattern will match:

Alan
alan
alaska

but will not match

Alight
alignment
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question