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)