0

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

1
  • Are you expecting nested [ ] inside your top level of [ ] ? Commented Nov 24, 2010 at 22:30

3 Answers 3

5
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.

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

Comments

1
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)

1 Comment

Sorry, I edited your answer by mistake, meant to edit my own. Reverted my change.
0

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

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.