Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>import re txt = '#TR=[ dfgg ] a kuku #TR=[ala ma kota]' </code></pre> <p>If you want to search for just the first occurrence of this pattern, use:</p> <pre><code>matches = re.search('#TR=\[([^\]]*)\]', txt) if matches: print(repr(matches.group(1))) ' dfgg dfg ' </code></pre> <p>If you want to find all occurrences in the text, use:</p> <pre><code>matches = re.findall('#TR=\[([^\]]*)\]', txt) if matches: print(matches) [' dfgg ', 'ala ma kota'] </code></pre> <p>Remember to check whether the characters you are searching for have <a href="http://docs.python.org/library/re.html#regular-expression-syntax" rel="nofollow">special meaning</a> in regular expressions (like <code>[</code> or <code>]</code>). If they are special, escape them with the backslash: <code>\[</code>.</p> <p>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 <code>.*</code> (which means "match any character except newline"; <a href="http://docs.python.org/library/re.html#re.DOTALL" rel="nofollow">details</a>) instead of <code>[^\]]*</code> (which means "match until the <code>]</code> is found, and stop before it"), too much text could be matched:</p> <pre><code>matches = re.findall('#TR=\[(.*)\]', txt) if matches: print(matches) [' dfgg ] a kuku #TR=[ala ma kota'] </code></pre> <p>You can also use the "non-greedy" modifier <code>?</code> in your pattern, after the qualifier (<code>*</code>, <code>+</code>) which enables the "the-less-characters-the-better" matching (use <code>*?</code>, <code>+?</code>). The result could be more readable:</p> <pre><code>'#TR=\[(.*?)\]' </code></pre> <p>instead of:</p> <pre><code>'#TR=\[([^\]]*)\]' </code></pre> <p>There's a great online tool to test your patterns as-you-type: <a href="http://gskinner.com/RegExr/" rel="nofollow">RegExr by Grant Skinner</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload