Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First make sure you're using the correct operator: <code>=~</code> is correct, <code>~=</code> is not.</p> <p>The operator <code>=~</code> returns the index of the first match (<code>nil</code> if no match) and stores the <code>MatchData</code> in the global variable <code>$~</code>. Named capture groups are assigned to a hash on <code>$~</code>, and, when the <code>RegExp</code> is a literal on the left side of the operator, are also assigned to local variables with those names.</p> <pre><code>&gt;&gt; str = "Here is a string" &gt;&gt; re = /(?&lt;vowel&gt;[aeiou])/ # Contains capture group named "vowel" &gt;&gt; str =~ re =&gt; 1 &gt;&gt; $~ =&gt; #&lt;MatchData "e" vowel:"e"&gt; &gt;&gt; $~[:vowel] # Accessible using symbol... =&gt; "e" &gt;&gt; $~["vowel"] # ...or string =&gt; "e" &gt;&gt; /(?&lt;s_word&gt;\ss\w*)/ =~ str =&gt; 9 &gt;&gt; s_word # This was assigned to a local variable =&gt; " string" </code></pre> <p>The method <code>match</code> returns the <code>MatchData</code> itself (again, <code>nil</code> if no match). Named capture groups in this case, on either side of the method call, are assigned to a hash on the returned <code>MatchData</code>.</p> <pre><code>&gt;&gt; m = str.match re =&gt; #&lt;MatchData "e" vowel:"e"&gt; &gt;&gt; m[:vowel] =&gt; "e" </code></pre> <p>See <a href="http://www.ruby-doc.org/core-1.9.3/Regexp.html" rel="noreferrer">http://www.ruby-doc.org/core-1.9.3/Regexp.html</a> (as well as the sections on <code>MatchData</code> and <code>String</code>) for more details.</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