简体   繁体   English

正则表达式模式以提取子字符串

[英]Regex pattern to extract substring

mystring = "q1)whatq2)whenq3)where" mystring = "q1)whatq2)whenq3)where"

want something like ["q1)what", "q2)when", "q3)where"] 想要类似["q1)what", "q2)when", "q3)where"]

My approach is to find the q\\d+\\) pattern then move till I find this pattern again and stop. 我的方法是找到q\\d+\\)模式,然后移动直到再次找到该模式并停止。 But I'm not able to stop. 但是我无法停止。

I did req_list = re.compile("q\\d+\\)[*]\\q\\d+\\)").split(mystring) 我做了req_list = re.compile("q\\d+\\)[*]\\q\\d+\\)").split(mystring)

But this gives the whole string. 但这给出了整个字符串。 How can I do it? 我该怎么做?

You could try the below code which uses re.findall function, 您可以尝试以下使用re.findall函数的代码,

>>> import re
>>> s = "q1)whatq2)whenq3)where"
>>> m = re.findall(r'q\d+\)(?:(?!q\d+).)*', s)
>>> m
['q1)what', 'q2)when', 'q3)where']

Explanation: 说明:

  • q\\d+\\) Matches the string in the format q followed by one or more digits and again followed by ) symbol. q\\d+\\)匹配格式为q的字符串,后跟一个或多个数字,然后再跟)符号。
  • (?:(?!q\\d+).)* Negative look-ahead which matches any char not of q\\d+ zero or more times. (?:(?!q\\d+).)*负向超前匹配,将匹配q\\d+任何char零次或多次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM