python正则表达式匹配手机号末尾两个数字重复_python – 正则表达式包括重叠匹配相同的开......

正如我上面所说的,正则表达式是一种主要的线性和单一规则的引擎 – 你可以选择贪婪捕获与否,但你不能同时选择它们.此外,大多数正则表达式引擎不支持重叠匹配(甚至那些支持它的人用子串/强制移动来伪造它)因为它也不适合正则表达式哲学.

如果您只查看两个子串之间的简单重叠匹配,可以自己实现:

def find_substrings(data, start, end):

result = []

s_len = len(start) # a shortcut for `start` length

e_len = len(end) # a shortcut for `end` length

current_pos = data.find(start) # find the first occurrence of `start`

while current_pos != -1: # loop while we can find `start` in our data

# find the first occurrence of `end` after the current occurrence of `start`

end_pos = data.find(end, current_pos + s_len)

while end_pos != -1: # loop while we can find `end` after the current `start`

end_pos += e_len # just so we include the selected substring

result.append(data[current_pos:end_pos]) # add the current substring

end_pos = data.find(end, end_pos) # find the next `end` after the curr. `start`

current_pos = data.find(start, current_pos + s_len) # find the next `start`

return result

哪个会产生:

substrings = find_substrings("BADACBA", "B", "A")

# ['BA', 'BADA', 'BADACBA', 'BA']

但是你必须修改它以获得更复杂的匹配.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用re模块来进行正则表达式匹配。要进行正则表达式匹配,可以使用re.match、re.search或re.findall等函数。re.match函数从字符串的始位置匹配正则表达式,re.search函数在字符串中寻找可以匹配的子串,而re.findall函数寻找所有匹配正则表达式的子串并返回一个列表。 在使用正则表达式时,有一些特殊字符需要注意转义,例如\、^、$等。为了方便编写正则表达式且不考虑转义问题,可以在正则表达式前加上前缀r,例如r'^py\001&'。 此外,正则表达式中还有一些特殊的字符类别,例如\s匹配任何空白字符,\d匹配一个数字,\D匹配一个非数字等。还可以使用限定符如*、+、?来指定匹配次数。 在使用正则表达式时,可以使用分组、替换等功能。re.compile函数可以编译正则表达式,re.split函数可以根据正则表达式进行切分字符串,re.sub函数可以将匹配到的子串替换为指定的字符串。 综上所述,Python中的正则表达式匹配可以通过re模块提供的函数来实现,具体的匹配方法和功能可以根据需要选择使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python 正则匹配](https://blog.csdn.net/weixin_42553458/article/details/90729738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python 正则表达式匹配](https://blog.csdn.net/qq_40727946/article/details/103566439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值