卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

我们如何在Python的正则表达式中找到每个匹配的精确位置?

我们如何在Python的正则表达式中找到每个匹配的精确位置?

介绍

re模块是我们在Python中使用的正则表达式。文本搜索和更复杂的文本操作都使用正则表达式。像grep和sed这样的工具,像vi和emacs这样的文本编辑器,以及像Tcl、Perl和Python这样的计算机语言都内置了正则表达式支持。

Python中的re模块提供了用于匹配正则表达式的函数。

定义我们要查找或修改的文本的正则表达式称为模式。文本字面量和元字符构成了这个字符串。编译函数用于创建模式。建议使用原始字符串,因为正则表达式经常包含特殊字符。(r字符用于指示原始字符串。)这些字符在组合成模式之前不会被解释。

可以使用其中一个函数将模式应用于文本字符串,模式在组装完成后使用。可用的函数包括Match、Search、Find和Finditer。

使用的语法

在这里使用的正则表达式函数是:我们使用正则表达式函数来查找匹配项。

re.match(): Determines if the RE matches at the beginning of the string. If zero or more characters at the beginning of the string match the regular expression pattern, the match method returns a match object.

p.finditer(): Finds all substrings where the RE matches and returns them as an iterator. An iterator delivering match objects across all non-overlapping matches for the pattern in a string is the result of the finditer method.

re.compile(): Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search(), and other methods described below. The expression’s behavior can be modified by specifying a flag's value. Values can be any of the following variables combined using bitwise OR (the | operator).

m.start(): m.start() returns the offset in the string at the match's start.

m.group(): You may use the multiple-assignment approach to assign each value to a different variable when mo.groups() returns a tuple of values, as in the areaCode, mainNumber = mo.groups() line below.

search: It is comparable to re.match() but does not require that we just look for matches at the beginning of the text. The search() function can locate a pattern in the string at any location, but it only returns the first instance of the pattern.

算法

  • 使用import re导入正则表达式模块。

  • 使用re.compile()函数创建一个正则表达式对象。(记得使用原始字符串。)

  • 将要搜索的字符串传递给Regex对象的finditer()方法。这将返回一个Match对象。

  • 调用Match对象的group()方法返回实际匹配的文本字符串。

  • 我们还可以使用span()方法在一个元组中获取起始和结束索引。

Example

的翻译为:

例子

 
#importing re functions
import re
#compiling [A-Z0-9] and storing it in a variable p
p = re.compile("[A-Z0-9]")
#looping m times in p.finditer
for m in p.finditer('A5B6C7D8'):
#printing the m.start and m.group
   print m.start(), m.group()

输出

这将产生输出−

0 A
1 5
2 B
3 6
4 C
5 7
6 D
7 8

代码解释

使用import re导入正则表达式模块。使用re.compile()函数创建一个正则表达式对象(“[A-Z0-9]”)并将其赋值给变量p。使用循环遍历m,并将要搜索的字符串传递给正则表达式对象的finditer()方法。这将返回一个Match对象。调用Match对象的m.group()和m.start()方法以返回实际匹配文本的字符串。

Example

的翻译为:

例子

# Python program to illustrate
# Matching regex objects
# with groups
import re
phoneNumRegex = re.compile(r'(ddd)-(ddd-dddd)')
mo = phoneNumRegex.search('My number is 415-555-4242.')
print(mo.groups())

输出

这将产生输出−

('415', '555-4242')

代码解释

使用import re导入正则表达式模块。使用re.compile()函数创建一个正则表达式对象(r'(ddd)-(ddd-dddd)'),并将其赋值给变量phoneNumRegex。将要搜索的字符串传递给Regex对象的search()方法,并将其存储在变量mo中。这将返回一个Match对象。调用Match对象的mo.groups()方法以返回实际匹配的文本字符串。

结论

Python re模块提供的search()、match()和finditer()方法允许我们匹配正则表达式模式,并且如果匹配成功,它会提供Match对象实例。使用这个Match对象的start()、end()和span()方法来获取关于匹配字符串的详细信息。

当有很多匹配项时,如果使用findall()将它们全部加载,您可能会面临内存过载的风险。您可以通过使用finditer()方法获得所有潜在匹配项的迭代器对象,这将提高效率。

这意味着finditer()提供了一个可调用对象,当调用时,将结果加载到内存中。

卓越飞翔博客
上一篇: PHP Fatal error: Class 'classname' not found解决方法
下一篇: 解决golang报错:unreachable code,解决方法
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏