Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two issues going on, here. You used <a href="http://docs.python.org/library/re.html#re.MatchObject.group" rel="nofollow">group()</a> without specifying a group, and I can tell you are getting confused between the behavior of regular expressions <em>with</em> an explicitly parenthesized group and <em>without</em> a parenthesized group. This behavior <em>without</em> parentheses that you are observing is just a shortcut that Python provides, and you need to read the documentation on <a href="http://docs.python.org/library/re.html#re.MatchObject.group" rel="nofollow">group()</a> to understand it fully.</p> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; string = "baaa" &gt;&gt;&gt; &gt;&gt;&gt; # Here you're searching for one or more `a`s until the end of the line. &gt;&gt;&gt; pattern = re.search(r"a+$", string) &gt;&gt;&gt; pattern.group() 'aaa' &gt;&gt;&gt; &gt;&gt;&gt; # This means the same thing as above, since the presence of the `$` &gt;&gt;&gt; # cancels out any meaning that the `?` might have. &gt;&gt;&gt; pattern = re.search(r"a+?$", string) &gt;&gt;&gt; pattern.group() 'aaa' &gt;&gt;&gt; &gt;&gt;&gt; # Here you remove the `$`, so it matches the least amount of `a` it can. &gt;&gt;&gt; pattern = re.search(r"a+?", string) &gt;&gt;&gt; pattern.group() 'a' </code></pre> <p>Bottom line is that the string <code>a+?</code> matches one <code>a</code>, period. However, <code>a+?$</code> matches <code>a</code>'s <em>until the end of the line</em>. Note that without explicit grouping, you'll have a hard time getting the <code>?</code> to mean anything at all, ever. In general, it's better to be explicit about what you're grouping with parentheses, anyway. Let me give you an example <em>with</em> explicit groups.</p> <pre><code>&gt;&gt;&gt; # This is close to the example pattern with `a+?$` and therefore `a+$`. &gt;&gt;&gt; # It matches `a`s until the end of the line. Again the `?` can't do anything. &gt;&gt;&gt; pattern = re.search(r"(a+?)$", string) &gt;&gt;&gt; pattern.group(1) 'aaa' &gt;&gt;&gt; &gt;&gt;&gt; # In order to get the `?` to work, you need something else in your pattern &gt;&gt;&gt; # and outside your group that can be matched that will allow the selection &gt;&gt;&gt; # of `a`s to be lazy. # In this case, the `.*` is greedy and will gobble up &gt;&gt;&gt; # everything that the lazy `a+?` doesn't want to. &gt;&gt;&gt; pattern = re.search(r"(a+?).*$", string) &gt;&gt;&gt; pattern.group(1) 'a' </code></pre> <p><sub>Edit: Removed text related to old versions of the question.</sub></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. This table or related slice is empty.
    1. VO
      singulars
      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