当前位置:   article > 正文

Python正则表达式操作–正则表达式

python中matches.group(1)

The regular expression is a popular topic in system administrators and developers. A regular expression is used to find structured text or string in single or multiple files. The best side of regular expression we can define whatever we want to match string in texts. Python supports and provides a lot of methods for regular expressions and related operations. In this tutorial, we will look at these regex functions in detail.

正则表达式是系统管理员和开发人员中的热门话题。 正则表达式用于在单个或多个文件中查找结构化文本或字符串。 正则表达式的最佳方面是,我们可以定义我们想要匹配文本中的字符串的任何内容。 Python支持并提供许多用于正则表达式和相关操作的方法。 在本教程中,我们将详细研究这些正则表达式函数。

导入Re或正则表达式库 (Import Re or Regular Expression Library)

In order to work with regular expressions in python, we need to import regular expression library which is named as a shortcut of regular expression as regex .

为了与Python正则表达式的工作,我们需要导入它命名为快捷方式正则表达式库regular expressionregex

import regex

比赛 (Match)

The match function is one of the most popular functions which will apply regex pattern into the given string. We will use match function with pattern and string parameters. There is also flags parameter which can be used to provide some flags like the case, interpretation, etc. If we do not provide flags there will be no error.

match函数是最受欢迎的函数之一,它将正则表达式模式应用到给定的字符串中。 我们将match函数与patternstring参数一起使用。 还有flags参数,可用于提供一些标志,例如大小写,解释等。如果我们不提供flags则不会有错误。

re.match(PATTERN,STRING,FLAG)

In this example, we want to find words that are delimited by spaces in the given string. Each word provides single match and those matches will be grouped.

在此示例中,我们要查找由给定字符串中的空格分隔的单词。 每个单词提供单个匹配项,这些匹配项将被分组。

  1. line="This is an example about regular expression"
  2. matches = re.match('\w+',line)
  3. matches.group(0)

团体 (Groups)

In the previous part, we have simply printed the first group which index is   but we may have more than one word to match in a line. It is called a group in the regex. We can match multiple different patterns in a single match.

在上一部分中,我们仅打印了索引为但是我们可能有多个单词要匹配。 它在正则表达式中称为组。 我们可以在一次匹配中匹配多个不同的模式。

In this example we will match words starts with T and a into two groups.

在此示例中,我们将以Ta开头的单词匹配为两组。

  1. line="This is an example about regular expression"
  2. matches = re.match('(T\w+).*example\s(a\w+)',line)
  3. matches.group(0)
  4. #'This is an example about'
  5. matches.group(1)
  6. #'This'
  7. matches.group(2)
  8. #'about'
Groups
Groups
团体

As we see matched pattern results are assigned into groups. We can get them by providing an index about these groups.

如我们所见,匹配的模式结果被分为几组。 我们可以通过提供有关这些组的索引来获取它们。

搜索 (Search)

Search is similar to the match function but the main difference is match looks up to the first match and then stops but the search will look at to the end of the string and will find multiple matches if exists. The syntax of the search function is the same match functions.

搜索类似于匹配功能,但主要区别是匹配查找到第一个匹配项,然后停止,但搜索将查找到字符串的末尾,如果存在则查找多个匹配项。 search功能的语法与match功能相同。

LEARN MORE  locate Command Tutorial With Examples For Linux To Find Files
了解更多信息查找带有Linux查找文件示例的命令教程

re.search(PATTERN,STRING,FLAG)

研究(样式,字符串,标志)

  1. line="This is an example about regular expression"
  2. matches = re.search('(T\w+).*example\s(a\w+)',line)
  3. matches.group(0)
  4. #'This is an example about'
  5. matches.group(1)
  6. #'This'
  7. matches.group(2)
  8. #'about'

搜索和替换 (Search and Replace)

Python regex functions support finding given text and replacing the text with a new one. We will use sub functions in order to replace. sub function supports the following syntax.

Python regex函数支持查找给定的文本并将其替换为新文本。 我们将使用sub来替换。 sub支持以下语法。

re.sub(PATTERN,NEWTEXT,STRING,FLAG)

We will change regular word with unregular word in this example.

在此示例中,我们将用unregular词更改regular词。

  1. line="This is an example about regular expression"
  2. matches = re.sub('regular','unregular',line)
  3. print(matches)
Search and Replace
Search and Replace
搜索和替换

选项标志(Option Flags)

Options flags generally provided as last parameter to the related regex functions. Option flags generally used to case-insensitive match, interpret with current locale etc. Here is a list of option flags.

选项标志通常作为相关正则表达式功能的最后一个参数提供。 选项标记通常用于不区分大小写的匹配,使用当前语言环境进行解释等。这是选项标记的列表。

  • re.I is used case-insensitive match

    re.I用于不区分大小写的匹配

  • re.L is used for current locale

    re.L用于当前语言环境

  • re.M makes $ match end of line

    re.M使$ match在行尾

  • re.S makes . match any character, including newline

    re.S . 匹配任何字符,包括换行符

不区分大小写(Case Insensitive)

We can use option flags in order to make case-insensitive match or search with regular expression. We will provide re.I as last arguments to the relevant function like below.

我们可以使用选项标志来进行不区分大小写的匹配或使用正则表达式进行搜索。 我们将提供re.I作为相关函数的最后一个参数,如下所示。

matches = re.sub('regular','unregular',line,re.I)

翻译自: https://www.poftut.com/python-regular-expression-operations-regex/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/738459
推荐阅读
相关标签
  

闽ICP备14008679号