8

I want to match a string like 19740103-0379 or 197401030379, i.e the dash is optional. How do I accomplish this with regexp?

2
  • Specify the language and/or regex library you're using. Aug 20, 2010 at 7:48
  • 2
    You could use [0-9]{8}-?[0-9]{4}
    – user196106
    Aug 20, 2010 at 7:49

2 Answers 2

7

Usually you can just use -?. Alternatively, you can use -{0,1} but you should find that ? for "zero or one occurrences of" is supported just about everywhere.

pax> echo 19740103-0379 | egrep '19740103\-?0379'
19740103-0379

pax> echo 197401030379 | egrep '19740103\-?0379'
197401030379

If you want to accept 12 digits with any number of dashes in there anywhere, you might have to do something like:

-*([0-9]-*){12}

which is basically zero or more dashes followed by 12 occurrences of (a digit followed by zero or more dashes) and will capture all sorts of wonderful things like:

--3-53453---34-4534---

(of course, you should use \d instead of [0-9] if your regex engine has support for that).

1
  • 2
    You only need to escape - in a character class ([...]). A dash has no special meaning elsewhere in a regex.
    – Richard
    Aug 20, 2010 at 7:34
1

You could try different ones:

\d* matches a string consisting only of digits

\d*-\d* matches a string of format digits - dash - digits

[0-9\-]* matches a string consisting of only dashes and digits

You can combine them via | (or), so that you have for example (\d*)|(\d*-\d*): matches formats just digits and digits-dash-digits.

Not the answer you're looking for? Browse other questions tagged or ask your own question.