Solved

Easy regex for someone who knows how to do them!

Posted on 2006-11-20
4
215 Views
Last Modified: 2010-04-23
I have the following input , pattern and replacement:

input = "/suo/west_midlands/birmingham.aspx?"
pattern = "/suo/(\w+)/(\w+).aspx?"
replacement = "$1|$2"

when i run the match i get:

"west_midlands|birmingham?"

which is correct but when i change the underscore to be a dash it does not work!

as what i really want is a match for  :

 "/suo/west-midlands/birmingham.aspx?"   --->    "west-midlands|birmingham?"

Can anyone spot what im missing and if so, could you explain why the pattern i currently have does not work for - but does for _ and also what is the correct pattern.

Thanks in advance

Dave
0
Comment
Question by:schott19
  • 3
4 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 75 total points
ID: 17982311
Use this pattern for the one you are using.

     pattern = "/suo/([a-zA-Z_\-0-9]+)/([a-zA-Z_\-0-9]+).aspx?"

Fernando
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17982361
Hi schott19;

The reason why the pattern you are using is not giving you the results you want is because the \w meta-character has the following meaning:

Match any of the following character, a-z, A-Z, _, and 0-9.

Therefore the character, -, is not a member of the set and is not a match. This is the reason why I replaced the \w with [a-zA-Z_\-0-9]. Also note that the - is escaped in the pattern because when it appears inside [ ] it is a range meta-character.

Fernando
0
 
LVL 3

Author Comment

by:schott19
ID: 17982392
thanks for your help,

Works a treat now... i was almost there but i forgot the + after the [ ]
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17982435
Not a problem, glad I was able to help. ;=)
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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

22 Experts available now in Live!

Get 1:1 Help Now