Pol
Pol

Reputation: 25535

Python. Regular expression

How to find everything which goes after symols #TR= and it is inside [ ] using re module. For example #TR=[ dfgg dfgddfg dgfgf dgdgdg dfgfg ]

Upvotes: 0

Views: 235

Answers (3)

Marek Augustyn
Marek Augustyn

Reputation: 146

import re
txt = '#TR=[ dfgg ] a kuku #TR=[ala ma kota]'

If you want to search for just the first occurrence of this pattern, use:

matches = re.search('#TR=\[([^\]]*)\]', txt)
if matches:
    print(repr(matches.group(1)))
' dfgg dfg '

If you want to find all occurrences in the text, use:

matches = re.findall('#TR=\[([^\]]*)\]', txt)
if matches:
    print(matches)
[' dfgg ', 'ala ma kota']

Remember to check whether the characters you are searching for have special meaning in regular expressions (like [ or ]). If they are special, escape them with the backslash: \[.

Also remember, that by default, regular expressions are "greedy" which means they try to get as much text to match the pattern as possible; so if you use .* (which means "match any character except newline"; details) instead of [^\]]* (which means "match until the ] is found, and stop before it"), too much text could be matched:

matches = re.findall('#TR=\[(.*)\]', txt)
if matches:
    print(matches)
[' dfgg ] a kuku #TR=[ala ma kota']

You can also use the "non-greedy" modifier ? in your pattern, after the qualifier (*, +) which enables the "the-less-characters-the-better" matching (use *?, +?). The result could be more readable:

'#TR=\[(.*?)\]'

instead of:

'#TR=\[([^\]]*)\]'

There's a great online tool to test your patterns as-you-type: RegExr by Grant Skinner.

Upvotes: 5

John Keyes
John Keyes

Reputation: 5604

import re
# compile the regex
exp = re.compile('.*\[(.*)\].*')
txt = r"#TR=[ dfgg dfgddfg dgfgf dgdgdg dfgfg ]"
match = exp.match(txt)
# grab the text between the square brackets
result = match.group(1)

Upvotes: 1

alpha-mouse
alpha-mouse

Reputation: 5003

(?<=#TR=[)[^]]*(?=])

Upvotes: 0

Related Questions