Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Easy regex for someone who knows how to do them!

Posted on 2006-11-20
4
Medium Priority
?
225 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 64

Accepted Solution

by:
Fernando Soto earned 300 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 64

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 64

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

715 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