python – 正则表达式:如何匹配使用以前的匹配?

python – 正则表达式:如何匹配使用以前的匹配?,第1张

概述我正在搜索表单的字符串模式: XXXAXXX # exactly 3 Xs, followed by a non-X, followed by 3Xs 所有X必须是相同的字符,A不能是X. 注意:我没有明确搜索X和As – 我只需要找到这种字符模式. 是否可以使用正则表达式构建它?如果重要的话,我将用Python实现搜索. 提前致谢! -CS 更新: @ rohit-jain在Python中的回 我正在搜索表单的字符串模式:

XXXAXXX # exactly 3 Xs,followed by a non-X,followed by 3Xs@H_404_12@  

所有X必须是相同的字符,A不能是X.

注意:我没有明确搜索X和As – 我只需要找到这种字符模式.

是否可以使用正则表达式构建它?如果重要的话,我将用Python实现搜索.

提前致谢!
-CS

更新:

@ rohit-jain在Python中的回答

x = re.search(r"(\w)\1{2}(?:(?!\1)\w)\1{3}",data_str)@H_404_12@  

@jerry在Python中的回答

x = re.search(r"(.)\1{2}(?!\1).\1{3}",data_str)@H_404_12@解决方法 你可以试试这个:  

(\w)\1{2}(?!\1)\w\1{3}@H_404_12@  

分手:

(\w)        # Match a word character and capture in group 1\1{2}       # Match group 1 twice,to make the same character thrice - `XXX`(?!\1)      # Make sure the character in group 1 is not ahead. (X is not ahead)\w          # Then match a word character. This is `A` \1{3}       # Match the group 1 thrice - XXX@H_404_12@                            	          总结       

以上是内存溢出为你收集整理的python – 正则表达式:如何匹配使用以前的匹配?全部内容,希望文章能够帮你解决python – 正则表达式:如何匹配使用以前的匹配?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1194686.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存