简体   繁体   中英

python - Regex to match patterns from last

Hi I am stuck in extracting data,

import re
s = "this is the [[sample1]] string [[sample2]](explanation)"
re.findall("(?=\[\[)(.*)(?<=\))",s)

this results : ['[[sample1]] string [[sample2]](explanation)']

but i want to extract : [[sample2]](explanation)']

Kindly suggest a way to do this.

Thanks in advance !

One of the ways:

import re

s = "this is the [[sample1]] string [[sample2]](explanation)"
res = re.findall(r"\[\[[^(\[]+\([^()]+\)", s)
print(res)

The output:

['[[sample2]](explanation)']

This expression is also likely to work:

(\[\[[^\]]*\]\]\([^)]*\))

Test with re.findall

import re

regex = r"(\[\[[^\]]*\]\]\([^)]*\))"

test_str = """

this is the [[sample1]] string [[sample1]](explanation) this is the [[sample1]] string 

[[sample2]](explanation1) [[]]()

[[sample3]](explanation1) [[sample4]]()

"""


print(re.findall(regex, test_str, re.M))

Output

['[[sample1]](explanation)', '[[sample2]](explanation1)', '[[]]()', '[[sample3]](explanation1)', '[[sample4]]()']

The expression is explained on the top right panel of regex101.com , if you wish to explore/simplify/modify it, and in this link , you can watch how it would match against some sample inputs, if you like.

RegEx Circuit

jex.im visualizes regular expressions:

在此处输入图片说明

Not regex but:

s = "this is the [[sample1]] string [[sample2]](explanation)"
extract = (s[::-1] [ s[::-1].index(")noitanalpxe(") : s[::-1].index("[[") + 2 ])[::-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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