How to Set up an OR Regular Expression in Python 


               


How to Set up an OR Regular Expression in Python



Python


In this article, we show how to set up an OR regular expression in Python.

You would use an OR regular expression if you are looking within a string for one of several different characters or subexpressions.

In other words, you are looking for an expression that is just not one way. It can be in one of several ways. This is why you would use an OR statement. You are looking for one of several possible characters or subexpressions.

So in the world of regular expressions, the symbol for OR is |.

We will run a few examples so that you can see how to utilize an OR regular expression in actual code.

So let's go over an example right now.

We will write a regular expression that looks for the word, bad, bed, or bid within a string.

If it finds either bad, bed, or bid, it will be a match for the regex match.

So the following code to search a string for bad, bed, or bid is shown below.



So let's now go over this code.

re is the module in Python that allows us to use regular expressions. So we first have to import re in our code, in order to use regular expressions.

After this, we have a variable, phrase1, which contains the string that we want to search using regular expressions.

The string is, "There are 240 beds in this room"

As you see, this string has the word, 'bed' in it.

Therefore, this word, 'bed', should match our regex.

We then create a variable named patterns, which we assign to, [r'b[e|a|i]d']

We will now dissect this regular expression.

So we have, r'b

What this does is it looks for a word that starts with the letter, b

We then have, [e|a|i], which signifies that the next character after b that we want to find is either e,a, or i.

We then have d' which signifies that the next character is d. Therefore, we are looking bad, bed, or bid.

If you look at the above examples that have each of these words, you can see that the regular expression contains these words.

The last phrase, phrase4, contains neither of these words, so you can see that this returns an empty list.

Now let's examine an alternative way that we can write this code.

This regular expression above will only have lowercase characters returned. It will not return uppercase characters.

To get the full picture, let's look at a complete example.

This is shown in the code below.



Okay, so this is another way we can write a regular expression that matches the word, bad, bed, or bid.

The way before was shorter, but some people may be more comfortable with this method, as it seems easier to write and read.

It finds either the word, bad, bed, or bid in a string and returns it in a list.

One thing to mention. The above lists look for bad, bed, bid, in the text, not necessarily the singularly word by itself, but as long as those 3 letters appear in the text. This means, that, 'bedroom', would count, which may not be what we would want.

So let's now write code that looks for the word, bad, bed, or bid, singularly occurring alone.

We can do this with the following code shown below.



Now this regex only returns the word, bad, bed, or bid, by itself singularly. It doesn't return it if the 3 letters are in the context of another word such as bedroom, beds, or bids.

And this is how we can use a logical OR with regular expressions in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...