tags:

views:

1018

answers:

5

I generally stay away from regular expressions because I seldom find a good use for them. But in this case, I don't think I have choice.

I need a regex for the following situation. I will be looking at three character strings. It will be a match if the first character is 1-9 or the letters o,n,d (lower or upper) AND the second character is 1,2 or 3 and the third character is 0-9.

Can anybody help me out?

+4  A: 
[1-9ondOND][123][0-9]

I omitted the ^ and $ (beginning and end of string markers) because you said you'd have three-character strings, but there's no harm in including them, and they may improve speed, not that that'll be a big deal on such short input.

Of course, this assumes you're working in a language and locale where the uppercase equivalent of o, n, and d are O, N, and D. If not, you'll need to tell your regex interpreter to ignore case. The mechanism varies by language/framework.

For python, you'd use something like:

re.match('[1-9ond][123][0-9]', inputstring, re.IGNORECASE)

The re.match forces a match at the beginning of string, so you wouldn't need the ^ in any case.

Blair Conrad
Even better to include the IGNORECASE directive in the re itself: '(?i)[1-9ond][123][0-9]', possibly with the addition of "^" and "$" in their respective places: '(?i)^[1-9ond][123][0-9]$'
ΤΖΩΤΖΙΟΥ
I'm intrigued. Why better? So one doesn't have to learn how to do it on various platforms? I like the explicitness of re.IGNORECASE, but I realize that's just my taste... I'm keen to learn the advantages.
Blair Conrad
+2  A: 

Perl /^[1-9ondOND][1-3][0-9]$/

^ = at the start of the string, $ = end of string

codebunny
A: 

In a PREG-based system (most of them these days):

^(?:[1-9]|[ond])[1-3][0-9]$

Some systems require the start/end markers (PHP, Perl, but not .NET for instance), if yours does, it'd end up something like:

/^(?:[1-9]|[ond])[1-3][0-9]$/
Matthew Scharley
Wow did you make that more complicated than you need to...
jj33
...and not even correct since it doesn't handle case in-sensitivity.
jj33
The best way of handling case insensitivity is in regex options (`/i` typically, or with the appropriate flag in .NET, or...), not the regex itself.
Matthew Scharley
+9  A: 

Slight variation on a few other answers. Restrict the input to be exactly the matched text.


^[1-9ondOND][123][0-9]$
JaredPar
Instead of the `^` and `$` anchors, the OP might also find `\W` or `\s` useful on each end of the expression. `\W` is a non-word character (`\w` is its opposite and matches letters and numbers), and `\s` matches any whitespace character (similarly, `\S` matches non whitespace characters).
dash-tom-bang
+2  A: 

[1-9ond][123][0-9]

and here's a useful place to test out regexes.

Greg