Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Question on Regular Expression

by Anonymous Monk
on Dec 27, 2014 at 01:09 UTC ( [id://1111441]=note: print w/replies, xml ) Need Help??


in reply to Question on Regular Expression

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)

Replies are listed 'Best First'.
Re^2: Question on Regular Expression
by sjain (Initiate) on Dec 27, 2014 at 12:45 UTC

    What is the difference between them? The difference is code snippet 2 is using one more named capture <P_NEW_MON_CODE>. What output are they supposed to give? Code snippet 1 is gving output as (value 'RS S') Value of p_rootcode in Pattern 1 is : RS R Code snippet 2 is gving output as below (no value printed) Value of p_rootcode in Pattern 2 is : Why oneliner regex, why not you use /x? How can I hope to use regular expressions without creating illegible and unmaintainable code? Sorry about that. Why did you use named patterns with $^MATCH? And with code callbacks? Named patterns used for saving the sub matches so that it can be used in later part of the code. Why did you use \K twice? Please ignore this as even if \K is not used still both code snippets are behaving same. Have you heard of rxrx? In this example it doesnt help why the output values are different.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found