Solved

Regular Expressions - Why does this match?

Posted on 2010-08-26
5
210 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now