Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regex : Return match elements

by ravi45722 (Pilgrim)
on Jun 24, 2016 at 11:47 UTC ( [id://1166492]=perlquestion: print w/replies, xml ) Need Help??

ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying this regex. But not reached. Help me in solving this

my $str = <<EO_STR; must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_e +rror must-column08:See Here & must-column10:Delivered | must-column9:No_err +or EO_STR my @array = *some_regex*; foreach my $element (@array) { print "$element\n"; } Needed output: must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_error must-column08:See Here & must-column10:Delivered | must-column9:No_error

My trails :

 my @matches = $str =~ /(\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|](\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|]/g;

Replies are listed 'Best First'.
Re: Regex : Return match elements
by choroba (Cardinal) on Jun 24, 2016 at 12:16 UTC
    split is your friend. Introducing a capture group in its regex adds the captured separators to the result, but it also adds undefs when the other alternative matches, so you need grep to filter them out.
    my @array = grep defined, split / ([|&]) |\n/, $str;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      first I also think for split but I am loosing '&' '|' while spliting. But you did the magic. thanks
        Split uses a regex (regular expression) to decide when to create elements of the resulting array. The code uses & or | to decide when to "make a new array element". Normally, in most splits, it is desired to "throw away" the split characters. Classic is: split /\s+/, $line; to parse space separated tokens. The use of a capture group in the regex allows the "splitted upon tokens" to appear in the output. This is an unusual situation, but works here.
Re: Regex : Return match elements
by AnomalousMonk (Archbishop) on Jun 24, 2016 at 15:52 UTC

    I may be missing something, but this seems to be all about appropriately transforming in-place the  & | characters (and any surrounding whitespace). Nothing needs to be extracted.

    Script:

    use warnings; use strict; print qq{running under Perl version: $] \n\n}; my $str = <<EO_STR; must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_e +rror must-column08:See Here & must-column10:Delivered | must-column9:No_err +or EO_STR print qq{<<$str>>}; print qq{\n\n}; $str =~ s{ \s* ([&|]) \s* }{\n$1\n}xmsg; print qq{[[$str]]};

    Output:

    c:\@Work\Perl\monks\ravi45722>perl xform_str_1.pl running under Perl version: 5.008009 <<must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_e +rror must-column08:See Here & must-column10:Delivered | must-column9:No_err +or >> [[must-column08:Submit & must-column10:Delivered must-column08:Submit & must-column10:Delivered One & must-column9:No_error must-column08:See Here & must-column10:Delivered | must-column9:No_error ]]


    Give a man a fish:  <%-{-{-{-<

      I need the output as a array but its returning as A string.

        I assume from this that you have a solution and need no further advice.


        Give a man a fish:  <%-{-{-{-<

Re: Regex : Return match elements
by AnomalousMonk (Archbishop) on Jun 24, 2016 at 17:30 UTC
    my @matches = $str =~ /(\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|](\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|]/g;

    This doesn't address the basic problem set out in your OP, but here are a few notes about the regex quoted therefrom:

    • (\w+|[0-9]+)     Because  [0-9] is a subset of the  \w character class, and because the  \w term is first in the ordered alternation, this regex expression is exactly equivalent to  (\w+) (see Character Classes and other Special Escapes in perlre);
    • [&|\|]     The  | alternation metacharacter has no special meaning in a character class, nor do any of the other "standard" metacharacters, so this expression is exactly equivalent to  [&|] (there are metacharacters specific to character classes; see Using character classes in perlretut);
    • [&|\|]     This expression appears twice in the quoted regex. Whenever I see repetition, I think refactor. This could be defined once as
          my $sep = qr{ [&|] }xms;
      and interpolated as needed, and if it ever needs to change, you only need to worry about changing it in one place (DRY);
    • Take a little whitespace for thy eyes' sake: get to know and love the  /x modifier (see regex modifiers in perlre).

    All this allows you to rewrite the original
        my @matches = $str =~ /(\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|](\w+)-(\w+|[0-9]+):(\w+|[0-9]+)\s[&|\|]/g;
    statement as the considerably less eye-bezoggling (IMHO)
        my $sep = qr{ [&|] }xms;
        my @matches = $str =~ /(\w+) - (\w+) : (\w+) \s $sep (\w+) - (\w+) : (\w+) \s $sep/xg;
    Again, this doesn't actually fix the original problem, but at least it becomes easier to see the problem (again, IMHO).


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1166492]
Approved by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-24 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found