Regex (Regular Expression) OR Logic Alternation Tutorial with Examples – POFTUT

Regex (Regular Expression) OR Logic Alternation Tutorial with Examples


A regular expression is a formation in order to match different text or words or numbers according to the given regex pattern. OR is a logic term used to provide selection choice from multiple choices. In this tutorial, we will explain what is Regex OR logic and how can we implement Regex OR in different programming languages like JavaScript, Python, C#, and Java.

Regex “OR” Logic

As stated previously OR logic is used to provide alternation from the given multiple choices. Let’s start with a simple example like we want to match one of the following values.

Turkey England Usa Germany

If we want to OR the given values all of them will match with the following sentences. We will use the pipe or vertical bar or alternation operator those are the same | to provide OR logic like below. We will also use parenthesis for grouping the OR values.

(Turkey|England|Usa|Germany)

 

I live in Turkey

I live in England

I live in Usa

I live in Germany

But will not match with the following sentences.

I live in Spain

I live in Canada

I live in Italy

I live in China
Regex OR Alternation Match Example
Regex OR Alternation Match Example

Vertical Bar or Pipe Symbol or Alternation Operand

Before diving into more complex examples we need to explain the terms Vertical Bar, Pipe Symbol and Alternation Operand. Actually all of them refer to the same thing which is |and  this symbol is used to delimit the regex OR values from each other.

Match Complete Sentence By Using Regex OR

This example is very similar to the previous one. But we will match the complete sentence by using Regex OR operand. We will provide the static part of the sentence and then add the OR part where multiple values can be used. In the following example, we want to match all sentences those start with I live in and end one of the countries Turkey, England, Usa, Germany. The regex expression will be as below.

I live in (Turkey|England|Usa|Germany)
Match Complete Sentence By Using Regex OR
Match Complete Sentence By Using Regex OR

Let’s examine why they match or not match.

  • `I live in Turkey` match because the `I live in` part is the same and `Turkey` is in the OR list.
  • `I live at England` not matched because `I live at` do not match the static part event the `England` matches.
  • `I live in Usa` match because the `I live in` part is the same and `Usa` is in the OR list.
  • `I live in Germany` not matched because `Germanny` do not match the OR list  `Germany`.
  • `I live in Spain` not matched because `Spain` do not match the OR list.
  • `I live in England` match because the `I live in` part is the same and `England` is in the OR list.
  • `I live in Italy` not matched because `Italy` does not match the OR list.
  • `I live in Turkey Country`matched exception the `Country` part because it is not in the regular expression.
LEARN MORE  Ultimate Sed Tutorial With Examples

Limit Regex OR with Line End

Regular expression targets only provided regular expression and do not check previous or after characters. If we want to limit the end of the regex and be sure that the line ends we can add $ at the end of the regular expression. We will use the following regular expression.

I live in (Turkey|England|Usa|Germany)$
Limit Regex OR with Line End
Limit Regex OR with Line End

Match Multiple IP Addresses with Regex OR

Regex OR is very useful in order to match multiple IP addresses. We will use the following regular expression which will match the IP address those ends with 1 or 254. We added $ in order to prevent to match IP addresses like 12 19 etc.

IP Address is \d{1,3}\.\d{1,3}\.\d{1,3}\.(1|254)$
Match Multiple IP Addresses with Regex OR
Match Multiple IP Addresses with Regex OR 

Match Multiple URLs with Regex OR

Another useful usage of the regex OR is matching multiple web site domain names or URLs. In the following example, we will match the URLs which are about poftut.com and siberhavadis.com.

I love (poftut|www\.poftut|siberhavadis|www\.siberhavadis)\.com
Match Multiple URLs with Regex OR
Match Multiple URLs with Regex OR

Leave a Comment