Solved

perl regex match word that could have multiple hyphens

Posted on 2014-07-18
2
317 Views
Last Modified: 2014-07-18
I am having a difficult time trying to form a perl regular expression that would match a word (made up of alphanumeric characters and underscores) that contains multiple hyphens in different places. I can match with one hyphen, but when there are other hyphens I can not think of anything and have looked around the web and have found nothing that would work.

For example, the part of the regex I now have that will match with 1 hyphen is:  (\w+-*\w*)
This will match, for example, 'wry-29', but not 'wry-29_th-34'

What I am trying to match could have 4 or more hyphens scattered about in the 'word'.
I could just repeat the expression 4 times,  \w+-*\w*-*\w*-*\w*-*\w*, but there would end up being 5 hyphens in the next thing I work on, and I would like to find a more elegant solution.
0
Comment
Question by:vbaabv
2 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40205533
Use repetition of sets:
/\b[\w-]+\b/

Open in new window

0
 

Author Closing Comment

by:vbaabv
ID: 40205593
Works really well.  Great job !
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

Suggested Solutions

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,…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now