Solved

Easy regex for someone who knows how to do them!

Posted on 2006-11-20
4
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 63

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 63

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 63

Expert Comment

by:Fernando Soto
ID: 17982435
Not a problem, glad I was able to help. ;=)
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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