Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, if you are going to slurp files, you should use <a href="http://search.cpan.org/~drolsky/File-Slurp-9999.13/" rel="nofollow noreferrer">File::Slurp</a>. Then you can do:</p> <pre><code>my $contents = read_file $file; </code></pre> <p>read_file will croak on error.</p> <p>Second, [^typename] does not exclude just the string 'typename' but also any string containing any of those characters. Other than that, it is not obvious to me that the pattern you use will consistently match the things you want it to match, but I can't comment on that right now.</p> <p>Finally, to get all the matches in the file one by one, use the g modifier in a loop:</p> <pre><code>my $source = '3 5 7'; while ( $source =~ /([0-9])/g ) { print "$1\n"; } </code></pre> <p>Now that I have had a chance to look at your pattern, I am still not sure of what to make of [^typename], but here is an example program that captures the part between the angle brackets (as that seems to be the only thing you are capturing above):</p> <pre><code>use strict; use warnings; use File::Slurp; my $pattern = qr{ ^ \w+ &lt;\s*((?:\w+(?:,\s*)?)+)\s*&gt; \s* \w+\s*; }mx; my $source = read_file \*DATA; while ( $source =~ /$pattern/g ) { my $match = $1; $match =~ s/\s+/ /g; print "$match\n"; } __DATA__ CMyClass&lt;int&gt; myClassInstance; CMyClass2&lt; int, int &gt; myClass2Instacen; C:\Temp&gt; t.pl int int, int </code></pre> <p>Now, I suspect you would prefer the following, however:</p> <pre><code>my $pattern = qr{ ^ ( \w+ &lt;\s*(?:\w+(?:,\s*)?)+\s*&gt; \s* \w+ ) \s*; }mx; </code></pre> <p>which yields:</p> <pre><code>C:\Temp&gt; t.pl CMyClass&lt;int&gt; myClassInstance CMyClass2&lt; int, int &gt; myClass2Instacen </code></pre>
 

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