Solved

Regex to match a dash

Posted on 2010-11-23
15
772 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

706 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

20 Experts available now in Live!

Get 1:1 Help Now