JeffK
JeffK

Reputation: 105

Is it possible to have a single regular expression that matches against multiple expressions?

I have a date that I'm allowing to be in multiple formats:

24-01-12
january 24 2013
etc

is there a way to combine 2 or more distinct regular expressions into one using pipes or another method? I tried nesting brackets but this doesn't work, of course:

[regex | regex]

Upvotes: 1

Views: 53

Answers (1)

Cerbrus
Cerbrus

Reputation: 72947

Yea, try this:

(\d\d-\d\d-\d\d)|([a-z]*)

// So basically:
(regex)|(regex)

This matches all words, and the date in the first line.
(use (?:regex) if you want non-capturing groups, instead of capturing ones.)

If you were literally placing your regexes in the [], then that was the problem.

Upvotes: 2

Related Questions