Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>{n}</code> means "repeat the previous atom <code>n</code> times". In regular expressions, an atom is a self-contained unit. So a single character is an atom. So is a dot. A group is an atom as well (that contains other atoms), as is a character class. So <code>.{n}</code> means "match <code>n</code> characters" (because <code>.</code> means "match any character").</p> <p>Note that <code>{n}</code> is not like a backreference, in that it doesn't have to match the same text on each repetition. <code>.{5}</code> behaves exactly like <code>.....</code>.</p> <p>This construct is also more powerful. It can take two numbers, and it matches a repetition count for that whole range. So <code>.{3,5}</code> means "match 3 to 5 characters". And <code>.{3,}</code> means "match 3 or more characters". <code>?</code> can be replaced with <code>{0,1}</code>, <code>*</code> with <code>{0,}</code>, and <code>+</code> with <code>{1,}</code> if you so desired.</p> <hr> <p><code>?&lt;option:</code> isn't actually a thing. It's <code>(?&lt;option&gt;:&lt;pattern&gt;)</code>, and this turns on all the flags listed in <code>&lt;option&gt;</code> for the duration of <code>&lt;pattern&gt;</code>. It's like a group, except it doesn't actually create a back reference. So the expression <code>(?m:.)</code> means "match one character as if the flag <code>m</code> was turned on". Given the behavior of <code>m</code> as "match \n" as nhahtdh said in the comments, the expression <code>.(?m:.).</code> means "match any character besides newline, followed by any character, followed by any character besides newline".</p> <p>This construct has two benefits. First, it allows you to only have a flag apply to part of a pattern, which can be occasionally useful. And second, if you wrap your entire pattern in this construct, then you have control over the flags that apply to your regular expression regardless of where the expression is used. This is useful when you're providing the regex as a user and don't have control over the source of the program.</p> <hr> <p>Let's take a look at the examples you gave:</p> <pre><code>&gt; %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{2}(?m:.)\Z/ =&gt; ["fin\n", "fin\r\n", "find"] </code></pre> <p>Your pattern <code>/f.{2}(?m:.)\Z/</code> means "match f, followed by 2 of any character (but newline), followed by any character, and anchor to the end of the string or just before a newline".</p> <p>So in each of the 3 matches, <code>fin</code> matches the <code>f.{2}</code>. <code>(?m:.)</code> matches <code>\n</code> in the first, <code>\r</code> in the second, and <code>d</code> in the third. And <code>\Z</code> matches the end of the string in the first, just before a newline in the second, and the end of the string in the third.</p> <p><code>fi\n\n</code> doesn't match because the first <code>\n</code> here can't be matched by the <code>.</code> from <code>.{2}</code> without the <code>m</code> flag.</p> <pre><code>&gt; %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{1}(?m:.)\Z/ =&gt; ["fin\n", "fi\n\n"] </code></pre> <p>Here <code>fi</code> matches <code>f.{1}</code> in both cases. <code>(?m:.)</code> matches <code>n</code> and <code>\n</code>, and <code>\Z</code> matches before the newline in both cases.</p> <p><code>fin\r\n</code> doesn't match because <code>\Z</code> will only match before the final newline in the string, not before a CRLF pair. And <code>find</code> doesn't match because there's nothing to match the <code>d</code>.</p> <pre><code>&gt; %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.{1}(?m:.)\Z/ =&gt; [] </code></pre> <p>I think you have a copy &amp; paste error here. This is identical to the previous pattern and matches as that does.</p> <pre><code>&gt; %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\Z/ =&gt; ["fin\n", "fi\n\n"] </code></pre> <p>This is also identical to the previous pattern. <code>.</code> and <code>.{1}</code> are the same thing. In fact, <code>{1}</code> can always be stripped from any regular expression without changing anything.</p> <pre><code>&gt; %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(m:.)\Z/ =&gt; [] </code></pre> <p>You dropped the <code>?</code> in this pattern, changing the meaning of <code>(m:.)</code>. This no longer changes options. Now it's just a capturing group that matches the pattern <code>m:.</code>, which of course doesn't occur in your input.</p> <pre><code>&gt; %W{fin\n fi\n\n \n\n fin\r\n find}.grep /f.(?m:.)\z/ =&gt; [] </code></pre> <p>You changed <code>\Z</code> to <code>\z</code> here. The difference between those two is <code>\Z</code> may match before a trailing newline, but <code>\z</code> must only match the end of the string. Without being able to match before the trailing newline, none of your inputs here match. But, for example, if you had <code>fin</code> (without the newline) or <code>fi\n</code> (without the second newline) it would work.</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.
 

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