Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Regex to match a dash

Posted on 2010-11-23
15
Medium Priority
?
827 Views
Last Modified: 2012-06-27
I have been digging through trying to get a reg ex to match "-" more detailed something like:

2324sA-D13

That format doesnt really matter, but at least if it allows a dash, any number of dashes? Any ideas?

Thanks.
0
Comment
  • 7
  • 4
  • 4
15 Comments
 
LVL 3

Expert Comment

by:chrios
ID: 34197056
What language are you using?
Normally a dash does not need to be treated in any special way, so a pattern like "\w+-\w+" should work in that example. (Some dialects need the + to be escaped when used a s a multiplier.)

If that does not work, try adding a \ before -, like this: \w+\-\w+

Good luck.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34197076
I am working with JS
0
 
LVL 16

Expert Comment

by:Justin Mathews
ID: 34197099
If you are looking for something to match non-dash characaters followed by any number of  dash followed by non-dash characaters pattern use:

/[^\-]*(-[^\-]*)*/
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:chrios
ID: 34197109
In that case have a look at this page to test out your pattern:

http://www.regular-expressions.info/javascriptexample.html

\w+-\w+ matches your given pattern without grouping.
(\w+)-(\w+) Should work and place 2324sA and D13 in two groups that you can use.
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34197469
Thank you! I think that is really close, what about add alpha num along with dashes?

Then i think we're good.

Ryan
0
 
LVL 16

Assisted Solution

by:Justin Mathews
Justin Mathews earned 1000 total points
ID: 34197544
This should work for any number of dashes embedded in alpha-numeric characters:

/[\dA-Za-z]*(-[\dA-Za-z]*)*/
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34197643
I tried regex tester and used: 5436345A-AA - As an example and didnt pass. Right format correct?
0
 
LVL 16

Expert Comment

by:Justin Mathews
ID: 34197930
In the tester you should give it without // as:

[\dA-Za-z]*(-[\dA-Za-z]*)*
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34198024
Ahh...thanks!
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34198057
Sorry one last thing, is it possible to make it take only dashes and no: $ % ^ & * (  ! @, etc.

It worked great in the tester btw

Thanks
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34198067
So is it the "-" in the middle of the two groups that specifys that it CAN allow dashes?
0
 
LVL 16

Expert Comment

by:Justin Mathews
ID: 34198200
Yes. Currently it allows only alpha-nums and dashes.
0
 
LVL 3

Accepted Solution

by:
chrios earned 1000 total points
ID: 34198354
Just a quick note about the pattern.

[\dA-Za-z]*(-[\dA-Za-z]*)* also matches multiple dashes in a row, and also gives a match on an empty string. It is basically the same thing as writing only (-[\dA-Za-z]*)*

If you want to start with alphanum and only want one dash between the alphanumeric groups use the pattern [\dA-Za-z]+(-[\dA-Za-z]+)* instead.

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 34198945
@chrios, thank you for that.

Appreciate your help guys. Been dealing with alot of regex lately, starting to slowly but surely understand it. Still a bit intimidating, you guys seem like you have it down.

Thanks
0
 
LVL 3

Expert Comment

by:chrios
ID: 34199430
Glad to help, good luck.
0

Featured Post

Industry Leaders: 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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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,…
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…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

580 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