Hi everyone, I have a question related to regular expression and below are 2 code snippets. Can you please advise why they give diffferent output? What is wrong with code snippet 2 ?

What is the difference between them?

What output are they supposed to give?

Why oneliner regex, why not you use /x? How can I hope to use regular expressions without creating illegible and unmaintainable code?

Why did you use named patterns with $^MATCH? And with code callbacks?

Why did you use \K twice?

Have you heard of rxrx? It gives some indented explanations ... its close enough (or exact) to your pattern ...

(?<P_ROOTCODE> #### The start of a named capturing block (also + $1) .* #### Match any character (except newline), ze +ro-or-more times (as many possible) ) #### The end of the named capturing block (?{ push(@::rc1, ${^MATCH}) }) #### Execute a block of code \K #### Pretend the final match starts here (?{ $::p_rootcode = "@::rc1" }) #### Execute a block of code (?<DAY1> #### The start of a named capturing block (also + $2) [0-9] #### Match any of the listed characters ) #### The end of the named capturing block (?{ push(@::rc2, ${^MATCH}) }) #### Execute a block of code \K #### Pretend the final match starts here (?<P_MON_CODE> #### The start of a named capturing block (also + $3) [A-Z] #### Match any of the listed characters ) #### The end of the named capturing block (?{ push(@::rc3, ${^MATCH}) }) #### Execute a block of code $ #### Match only if at end of string (or final n +ewline)
(?<P_ROOTCODE> #### The start of a named capturing block (also + $1) .* #### Match any character (except newline), ze +ro-or-more times (as many as possible) ) #### The end of the named capturing block (?{ push(@::rc1, ${^MATCH}) }) #### Execute a block of code \K #### Pretend the final match starts here (?{ $::p_rootcode = "@::rc1" }) #### Execute a block of code (?<DAY1> #### The start of a named capturing block (also + $2) [0-9] #### Match any of the listed characters ) #### The end of the named capturing block (?{ push(@::rc2, ${^MATCH}) }) #### Execute a block of code \K #### Pretend the final match starts here (?<P_MON_CODE> #### The start of a named capturing block (also + $3) [A-Z] #### Match any of the listed characters ) #### The end of the named capturing block (?{ push(@::rc3, ${^MATCH}) }) #### Execute a block of code (?<P_NEW_MON_CODE> #### The start of a named capturing block (also $ +4) [A-Z] #### Match any of the listed characters ) #### The end of the named capturing block (?{ push(@::rc4, ${^MATCH}) }) #### Execute a block of code $ #### Match only if at end of string (or final n +ewline)

In reply to Re: Question on Regular Expression by Anonymous Monk
in thread Question on Regular Expression by sjain

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.