Regex to match a dash

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.
LVL 1
catonthecouchproductionsAsked:
Who is Participating?
 
chriosConnect With a Mentor Commented:
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
 
chriosCommented:
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
 
catonthecouchproductionsAuthor Commented:
I am working with JS
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Justin MathewsCommented:
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
 
chriosCommented:
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
 
catonthecouchproductionsAuthor Commented:
Thank you! I think that is really close, what about add alpha num along with dashes?

Then i think we're good.

Ryan
0
 
Justin MathewsConnect With a Mentor Commented:
This should work for any number of dashes embedded in alpha-numeric characters:

/[\dA-Za-z]*(-[\dA-Za-z]*)*/
0
 
catonthecouchproductionsAuthor Commented:
I tried regex tester and used: 5436345A-AA - As an example and didnt pass. Right format correct?
0
 
Justin MathewsCommented:
In the tester you should give it without // as:

[\dA-Za-z]*(-[\dA-Za-z]*)*
0
 
catonthecouchproductionsAuthor Commented:
Ahh...thanks!
0
 
catonthecouchproductionsAuthor Commented:
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
 
catonthecouchproductionsAuthor Commented:
So is it the "-" in the middle of the two groups that specifys that it CAN allow dashes?
0
 
Justin MathewsCommented:
Yes. Currently it allows only alpha-nums and dashes.
0
 
catonthecouchproductionsAuthor Commented:
@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
 
chriosCommented:
Glad to help, good luck.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.