Solved

Easy regex for someone who knows how to do them!

Posted on 2006-11-20
4
213 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Not a problem, glad I was able to help. ;=)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

19 Experts available now in Live!

Get 1:1 Help Now