Wickkiey
Wickkiey

Reputation: 4632

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 !

Upvotes: 0

Views: 42

Answers (3)

Emma
Emma

Reputation: 27723

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

Upvotes: 1

Adi219
Adi219

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

RomanPerekhrest
RomanPerekhrest

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

Related Questions