1

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?

6
  • 5
    You should really learn to use regular expressions. They are super-useful. Commented Oct 3, 2008 at 14:04
  • I second Kip's response. Oddly enough, the more you learn about them, the more useful they become. Commented Oct 3, 2008 at 14:06
  • Could you edit the title of your question to be a little more descriptive? Thanks in advance. Commented Oct 3, 2008 at 14:17
  • I would vote Kip's comment up if it would be possible. Regex (and pattern matching in general) is a sharp, powerful tool. Any time spent studying them it's well worth it! Commented Oct 3, 2008 at 14:34
  • Yes, please edit the title as suggested, and then remove the pointless text from the question about how you don't understand regex. Commented Oct 3, 2008 at 14:43

6 Answers 6

9

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


^[1-9ondOND][123][0-9]$
Sign up to request clarification or add additional context in comments.

1 Comment

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).
5
[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.

2 Comments

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.
2

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

^ at the start of the string;
$ at the end of the string.

Comments

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

Here's a useful place to test your RegEx.

1 Comment

@Greg This regex won't work if the first character is upper case letter
0

A very late answer, but hope this will help

([1-9]|(?i)(o|n|d))[123][\d]

http://regex101.com/r/vE2jT1/1

Comments

-2

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]$/

2 Comments

...and not even correct since it doesn't handle case in-sensitivity.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.