Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular Expressions - Why does this match?

Posted on 2010-08-26
5
Medium Priority
?
272 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:Alan
5 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 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 20

Author Closing Comment

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

Thanks,

Alan.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

824 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