Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perl can easily serve as your friend in cases like this. You can read the entire file into memory to apply a regular expression over multiple lines. Setting the input record separator to <code>0777</code> causes such a "slurping" action. The <code>-n</code> switch simply says to read the file or files provided on the command-line. The <code>-e</code> switch's argument constitutes the code to execute.</p> <p>The regular expression's <code>/s</code> modifier allows <code>.</code> to match a newline. The <code>\m</code> modifier allows both <code>^</code> and <code>$</code> to match immediately before and after an embedded newline. These are key to parsing a string containing more than one logical line. The <code>/g</code> modifier, tells the regex engine to search globally for all matches.</p> <pre><code>perl -0777 -ne 'print "$1 $2\n" while m{^(\S+).+?\[age=(\d+)\]}gms' file </code></pre> <p>Given an input file like this:</p> <pre><code>01238584 (other info) more info, more info [age=81][otherinfo][etc, etc] 98765432 (still other info) still more info, and more info [age=82][and more otherinfo][etc, etc, ad infinitum] </code></pre> <p>...the script above outputs:</p> <pre><code>01238584 81 98765432 82 </code></pre> <p>We can dissect the regular expression thusly:</p> <pre><code>perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain-&gt;new(qr/m{^(\S+). </code></pre> <p>+?[age=(\d+)]}gms/)->explain()'</p> <pre><code>The regular expression: (?-imsx:m{^(\S+).+?\[age=(\d+)\]}gms) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- m{ 'm{' ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- \S+ non-whitespace (all but \n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- .+? any character except \n (1 or more times (matching the least amount possible)) ---------------------------------------------------------------------- \[ '[' ---------------------------------------------------------------------- age= 'age=' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- \] ']' ---------------------------------------------------------------------- }gms '}gms' ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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