Solved

Regular Expressions - Why does this match?

Posted on 2010-08-26
5
197 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
Comment Utility
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
Comment Utility
.*la\++.*

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

Author Closing Comment

by:Alan3285
Comment Utility
It all seems so completely obvious when you explain it!

Thanks,

Alan.
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
thanks for the points :)
0
 

Expert Comment

by:AlJames
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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,…
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…

744 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

18 Experts available now in Live!

Get 1:1 Help Now