re.search()方法扫描整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None。

与re.match()方法不同,re.match()方法要求必须从字符串的开头进行匹配,如果字符串的开头不匹配,整个匹配就失败了;

re.search()并不要求必须从字符串的开头进行匹配,也就是说,正则表达式可以是字符串的一部分。

re.search(pattern, string, flags=0)
  • pattern : 正则中的模式字符串。
  • string : 要被查找替换的原始字符串。
  • flags : 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。
import re
content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.search('(\d+).*?(\d+).*', content)  
print(result)
print(result.group())    # print(result.group(0)) 同样效果字符串
print(result.groups())
print(result.group(1))
print(result.group(2))
<_sre.SRE_Match object; span=(6, 49), match='123456789 Word_This is just a test 666 Test'>
123456789 Word_This is just a test 666 Test
('123456789', '666')
123456789
Process finished with exit code 0

例2:只匹配数字

import re
content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.search('(\d+)', content)
print(result)
print(result.group())    # print(result.group(0)) 同样效果字符串
print(result.groups())
print(result.group(1))
<_sre.SRE_Match object; span=(6, 15), match='123456789'>
123456789
('123456789',)
123456789
Process finished with exit code 0

http://www.runoob.com/python/python-reg-expressions.html

re . match: 从头开始匹配, 使用group () 方法 可以获取第一个匹配值 re . search : 用包含方式匹配,使用group () 方法 可以获取第一个匹配值 re . findall: 用包含方式匹配,把所有匹配到的字符放到以列表中的元素返回多个匹配值 re . sub: 匹配字符并替换 re . split: 以匹配到的字符当做列表分隔符,返回列表 2 . Python 正则 . .
说到使用正则匹配字符串,就不得不说三个常用的匹配检索 方法 re . search () , re . match () re . findall () 。 主要的区别是前两个 方法 只在目标字符串中匹配一次满足条件的 正则表达式 ;而 re . findall () 方法 匹配目标字符串中所有满足条件的 正则表达式 ;另外 re . match () 只会匹配目标字符串开头是否满足 正则表达式 ,若开头不满足则匹配失败,函数返回None;而 re . search () 则不限位置。 至于具体的细节,请参考: Python 正则表达式 我这里想说的是 re . search () 方法
flags : 标志位,用于控制 正则表达式 的匹配方式,如:是否区分大小写,多行匹配等等。 re . search () 方法 扫描整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None。 re . search () 不要求必须从字符串的开头进行匹配,也就是说, 正则表达式 可以是字符串的一部分。 . . . python re . search re . match 一 re . search re . match python 提供了2中主要的 正则表达式 操作: re . match 和 re . search 。 match :只从字符串的开始与 正则表达式 匹配,匹配成功返回matchobj
Python 3 中, re . search () 函数用于在给定的字符串中搜索匹配 正则表达式 的第一个位置。这个函数接受两个参数,第一个参数是要搜索的 正则表达式 ,第二个参数是要搜索的字符串。 该函数返回一个匹配对象,你可以使用这个对象的group () 方法 来获取匹配的结果。<span class="em">1</span><span class="em">2</span><span class="em"> 3 </span> #### 引用[ . re fe re nce_title] - *1* [ Python 3 re . search () 方法 ](https://blog . csdn . net/m0_ 3 7 3 60684/article/details/8414040 3 )[target="_blank" data- re port-click={"spm":"1018 . 2226 . 3 001 . 96 3 0","extra":{"utm_source":"vip_chatgpt_common_ search _pc_ re sult","utm_medium":"distribute . pc_ search _ re sult . none-task-cask-2~all~insert_cask~default-1-null . 142^v9 3 ^chat search T 3 _1"}}] [ . re fe re nce_item style="max-width: 50%"] - *2* * 3 * [ python 正则表达式 re . search () 的使用](https://blog . csdn . net/qq_ 3 3 210042/article/details/11700 3 568)[target="_blank" data- re port-click={"spm":"1018 . 2226 . 3 001 . 96 3 0","extra":{"utm_source":"vip_chatgpt_common_ search _pc_ re sult","utm_medium":"distribute . pc_ search _ re sult . none-task-cask-2~all~insert_cask~default-1-null . 142^v9 3 ^chat search T 3 _1"}}] [ . re fe re nce_item style="max-width: 50%"] [ . re fe re nce_list ]