Solved

Regex to match a dash

Posted on 2010-11-23
15
785 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
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…

820 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