简体   繁体   中英

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 ]

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 .

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM