0

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 !

1
  • Can sample2 contain [ or ]? Can explanation contain ( or )? Commented Jul 21, 2019 at 20:45

3 Answers 3

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

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)']

Comments

0

Not regex but:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.