Regular expression using hypens and numbers

I want to create a regular expression in such a way that only hypens and digit should be allowed in the text box, criteria

  • Hypen should not be in the first and last position
  • Hypen must have numbers at both ends
  • The text field can contain n number of subscript numbers and digits.

Thank you in advance

+4
source share
7 answers

I can’t believe it, I got it, guessing the regular expression. Hope this works.

(\d+(\d*\\-\d+)+\d*)|\d+ 
+1
source

Here's a shortened version of the @El Yobo regex. You can replace [0-9] with \d , and you can make the hyphen optional with -? to remove the special case of idle strings.

 ^\d+(-?\d+)*$ 

http://ideone.com/SRqPW

+4
source

This regex should do this:

 ^[0-9]+(-[0-9]+)*$ 

This will correspond to one or more digits, followed by zero or more hyphen sequences, followed by one or more digits.

+3
source

I assume the empty string is valid. I'm not sure I understand your third article; do you mean that n can be anything, or do you need to limit things to n occurrences? I'm also not sure how many digits there should be at each end of the hyphen; is it any number one or more, or exactly one?

The following regular expression allows, for example, to use a string, for example, 1-9-129-2-293-23.

 ^(([0-9]+-[0-9]+)|[0-9]+)*$ 

Since each subpattern must begin and end with a number, there is no need to have a bit digit at each end outside the substring, as in other solutions posted here.

+2
source

Will this work?

 (\d+\-)*\d+ 

Edit: Changed "+" to "*" because hyphens do not seem necessary.

Edit2: Fixed regex to prevent double hyphens.

+2
source
 ^((\d+-)+\d+)*$ 

It says: you must start with a few numbers, and then - . Repeat as many times as you want, then you have to finish a few numbers. This is * at the end to allow blank lines.

+2
source

You can try using this regular expression:. .[\w-]*

-2
source

All Articles