正则表达式(正则表达式)或带有示例的逻辑替代教程

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.

正则表达式是一种结构,用于根据给定的正则表达式模式匹配不同的文本,单词或数字。 OR是一个逻辑术语,用于从多个选择中提供选择。 在本教程中,我们将解释什么是Regex OR逻辑以及如何以不同的编程语言(例如JavaScript,Python,C#和Java)实现Regex OR。

正则表达式“或”逻辑 (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.

如果我们想对给定值进行OR运算,则所有这些值都将与以下句子匹配。 我们将使用相同的管道或竖线或交替运算符| 提供如下所示的OR逻辑。 我们还将使用括号对OR值进行分组。

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

在深入研究更复杂的示例之前,我们需要解释术语“ Vertical Bar ,“ Pipe Symbol和“ Alternation Operand 。 实际上,所有这些都指的是| 并且此符号用于彼此分隔正则表达式OR值。

使用正则表达式匹配完整句子或 (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.

此示例与上一个示例非常相似。 但是我们将使用Regex OR操作数来匹配完整的句子。 我们将提供句子的静态部分,然后在可以使用多个值的位置添加“或”部分。 在下面的示例中,我们希望匹配所有以I live in的国家和结束于土耳其,英国,美国,德国之一的句子。 正则表达式将如下所示。

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.

    无法匹配“我住在意大利”,因为“意大利”与OR列表不匹配。
  • `I live in Turkey Country`matched exception the `Country` part because it is not in the regular expression.

    “我住在土耳其国家/地区匹配的例外”国家/地区部分,因为它不在正则表达式中。
LEARN MORE  How To Get Query String From Url With Javascript?
了解更多信息如何使用Javascript从网址获取查询字符串?

限制正则​​表达式或行尾 (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
限制正则​​表达式或行尾

使用Regex匹配多个IP地址或(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.

正则表达式OR对于匹配多个IP地址非常有用。 我们将使用以下正则表达式来匹配以1或254结尾的IP地址。我们添加了$以防止匹配12 19等IP地址。

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 
使用Regex匹配多个IP地址或 

使用正则表达式匹配多个URL或(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.

正则表达式OR的另一个有用用法是匹配多个网站域名或URL。 在以下示例中,我们将匹配关于poftut.comsiberhavadis.com的URL。

I love (poftut|www\.poftut|siberhavadis|www\.siberhavadis)\.com
Match Multiple URLs with Regex OR
Match Multiple URLs with Regex OR
使用正则表达式匹配多个URL或

翻译自: https://www.poftut.com/regex-regular-expression-or-logic-alternation-tutorial-with-examples/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值