tags:

views:

164

answers:

4
re.compile("abc")

I would like to do "abc" OR "xyz".

+11  A: 

Use |:

re.compile("abc|xyz")

It's worth perusing regular-expression.info for detailed information as well as Regular Expression HOWTO and re — Regular expression operations from the Python documentation.

cletus
+1  A: 

I'll take this opportunity to point you to an excellent reference for many of life's problems: Wikipedia.

Regular Expressions on Wikipedia

You might also find answers here.

ChessWhiz
A: 

I'm more a perl regex guy, but the current popular answer seems to match either abcyz or abxyz. I normally would have the regex look like "(abc)|(xyz)". You want to use the parenthesis to group the 2 strings your looking for.

devNoise
`abc|xyz` is correct on Python, Perl, and pretty much any other regex flavor that has `|`.
interjay
@interjay, that may be true, but grouping makes it easier to understand.
Michael Aaron Safyan
Using bare parentheses makes it a capturing group and thus potentially slower. If you want understandability, use re.VERBOSE.
John Machin
A: 

Remember the doc property inside of python. In particular, you can access it for regex by typing

import re 
re.__doc__

at the python shell or by using a python doc browser such as the one built into Spyder.

This particular question is addressed there.

TimothyAWiseman