Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this solution using <code>perl</code>. It can fail in many ways and have serious limitations, like no more than 9 alternatives, or no <code>|</code> in the expression. That's because the script surround each word in parentheses and looks for a match in <code>$1</code>, <code>$2</code>, etc.</p> <p>Content of <code>script.pl</code>:</p> <pre><code>#!/usr/bin/env perl use warnings; use strict; my (%matches, %words); die qq|Usage: perl $0 &lt;input-file&gt; &lt;regular-expression-PCRE&gt;\n| unless @ARGV == 2; my $re = pop; ## Assign an ordered number for each subexpression. do { my $i = 0; %words = map { ++$i =&gt; $_ } split /\|/, $re; }; ## Surround each subexpression between parentheses to be able to select them ## later with $1, $2, etc. $re =~ s/^/(/; $re =~ s/$/)/; $re =~ s/\|/)|(/g; $re = qr/$re/; ## Process each line of the input file. while ( &lt;&gt; ) { chomp; ## If it matches any of the alternatives, search for it in any of the ## grouped expressions (limited to 9). if ( m/$re/o ) { for my $i ( 1 .. 9 ) { if ( eval '$' . $i ) { $matches{ $i }++; } } } } ## Print them sorted. for my $key ( sort keys %matches ) { printf qq|%s\n|, $words{ $key } for ( 1 .. $matches{ $key } ); } </code></pre> <p>Assuming <code>infile</code> with data:</p> <pre><code>monkey donkey chicken horse dog cat chicken horse </code></pre> <p>Run it like:</p> <pre><code>perl script.pl infile 'horse|donkey|chicken' </code></pre> <p>That yields:</p> <pre><code>horse horse donkey chicken chicken </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