see Re: Count Quoted Words, Re: Help required in find command. (read parse file tokenize m//gc), Re^2: Help with regular expression ( m/\G/gc ), perlfaq6#What good is \G in a regular expression? , Re^2: POD style regex for inline HTML elements

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; sub TRACE; sub DEBUG; *TRACE = *DEBUG = sub { print STDERR @_,"\n" }; my $data =<<'END'; # yo x = 10; y = 12; z = 100; junk END my $matches = dadata( \$data ); dd( $matches ); while( @$matches ){ my $ma = shift @$matches ; dd( $ma ); } exit( 0 ); sub dadata { my( $dataref ) = @_; my @matches; pos( $$dataref ) = 0; while( length( $$dataref ) > pos( $$dataref ) ){ $$dataref =~ m{\G^(#.*$)}gcm and do { push @matches, [ "COMMENT", $1 ]; TRACE "# COMMENT $1"; next; };; $$dataref =~ m{\G^\s*(\w+)\s*=\s*(\d+)\s*;\s*$}gcmx and do { push @matches, [ "KV", $1 , $2 ]; TRACE "# K($1)=V($2)"; next; };; $$dataref =~ m{\G(\s+)}gcxs and do { push @matches, [ "SPACE", $1 ]; next; };; $$dataref =~ m{\G(\S)}gcxs and do { push @matches, [ "INCH", $1 ]; TRACE "# INCH($1)"; next; };; } return \@matches; } __END__ # COMMENT # yo # K(x)=V(10) # K(y)=V(12) # K(z)=V(100) # INCH(j) # INCH(u) # INCH(n) # INCH(k) [ ["COMMENT", "# yo"], ["SPACE", "\n"], ["KV", "x", 10], ["SPACE", "\n"], ["KV", "y", 12], ["SPACE", "\n"], ["KV", "z", 100], ["SPACE", "\n"], ["INCH", "j"], ["INCH", "u"], ["INCH", "n"], ["INCH", "k"], ["SPACE", "\n"], ] ["COMMENT", "# yo"] ["SPACE", "\n"] ["KV", "x", 10] ["SPACE", "\n"] ["KV", "y", 12] ["SPACE", "\n"] ["KV", "z", 100] ["SPACE", "\n"] ["INCH", "j"] ["INCH", "u"] ["INCH", "n"] ["INCH", "k"] ["SPACE", "\n"]

In reply to Re: Perl regex \G /g and /gc by Anonymous Monk
in thread Perl regex \G /g and /gc by tj_thompson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.