Solved

Regex to match a dash

Posted on 2010-11-23
15
783 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:jmatix
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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:jmatix
jmatix earned 250 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:jmatix
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:jmatix
ID: 34198200
Yes. Currently it allows only alpha-nums and dashes.
0
 
LVL 3

Accepted Solution

by:
chrios earned 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now