I have multiple strings such as
POST /incentivize HTTP/1.1
DELETE /interactive/transparent/niches/revolutionize HTTP/1.1
DELETE /virtual/solutions/target/web+services HTTP/2.0
PATCH /interactive/architect/innovative/24%2f7 HTTP/1.1
I want to target all these strings with regex
.
I tried the following pattern
pattern = r"([A-Z]* /([A-Za-z0-9])D+ [A-Z]*/d.d)"
Here is the full code
string = """
POST /incentivize HTTP/1.1
DELETE /interactive/transparent/niches/revolutionize HTTP/1.1
DELETE /virtual/solutions/target/web+services HTTP/2.0
PATCH /interactive/architect/innovative/24%2f7 HTTP/1.1
"""
pattern = r"(?P<url>[A-Z]* /([A-Za-z0-9])D+ [A-Z]*/d.d)"
result = [item.groupdict() for item in re.finditer(pattern,string)]
result
This outputs the following
[{'url': 'POST /incentivize HTTP/1.1'},
{'url': 'DELETE /interactive/transparent/niches/revolutionize HTTP/1.1'},
{'url': 'DELETE /virtual/solutions/target/web+services HTTP/2.0'}]
With this pattern, I am able to target the first three strings. But for the life of me, I am not able to figure out how to target the last string. This is just a sample of many more strings in the list. I need to make this dynamic so that the program is able to capture strings that are similar to this.
I am a rookie in python and have just started learning regex
.
Any help will be appreciated.
question from:
https://stackoverflow.com/questions/65649989/how-to-target-multiple-strings-with-single-regex-pattern 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…