Reputation: 4632
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 !
Upvotes: 0
Views: 42
Reputation: 27723
This expression is also likely to work:
(\[\[[^\]]*\]\]\([^)]*\))
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))
['[[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.
jex.im visualizes regular expressions:
Upvotes: 1
Reputation: 4814
Not regex but:
s = "this is the [[sample1]] string [[sample2]](explanation)"
extract = (s[::-1] [ s[::-1].index(")noitanalpxe(") : s[::-1].index("[[") + 2 ])[::-1]
Upvotes: 0
Reputation: 92854
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)']
Upvotes: 1