On 01/12/2016 12:47 PM, Bruce Gray wrote:


On Jan 11, 2016, at 6:55 PM, ToddAndMargo <toddandma...@zoho.com> wrote:

Would yo all terribly mind if I ask how to do this Perl 5 regex
in Perl 6?  (I learn best by example.)

if ( $ClickLine =~ /aes256/ and /${BaseTag}/ ) {
              push ( @WebClickHere, $ClickLine );

               if ( $Line =~ m{select id=\"(.*?)[-]} ) {
                  my $VerLine = $1;
                  push ( @WebVersions,  $VerLine );
               }
            }
—snip—

I don’t mind; I am happy that you have asked for the way you learn best :^)

You presented three regexes:
1.      $ClickLine =~ /aes256/
        $ClickLine ~~ /aes256/
Where you would use `=~` in Perl 5 to bind to a match or a subst,
you now use the `~~` smartmatch operator in Perl 6.
Yes, it is safe, even though smartmatch has some problems
in Perl 5; this is Perl 6's smartmatch!

2.      /${BaseTag}/
        /$BaseTag/          # Use for simple text
        /<$BaseTag>/        # Use for a full regular expression.
As FROGGS (Tobias Leich) said, the correct translation depends on
whether $BaseTag contains simple text, or should be interpreted as
a regex. Perl 5 always did the latter unless you used quotemeta().

3a.     $Line =~ m{select id=\"(.*?)[-]}
        $Line ~~  / select \s 'id="' (.*?) '-' /
* Space is now significant in Perl 6 regexes.
* Alphanum are literal text to match, or special if backslashed;
     `n` is just the character `n`, `\n` means `newline` .
* Non-alphanum are special, or literal text to match if backslashed or quoted;
     `+` means `one or more`, while the plus character is written as `\+` or 
`'+'` .
* Note: my translation is not tested.

3b.     my $VerLine = $1;
        my $VerLine = $0.Str;      # or ~$0 or $/[0].Str
* The capture vars ($1, $2...) have moved from being 1-based to 0-based; $1 is 
now $0, $2 is now $1, etc.
* Perl 5 capture vars held plain strings. In Perl 6, they hold Match objects, 
and must be stringified to behave like Perl 5.

See also:
     
http://docs.perl6.org/language/5to6-perlvar#Variables_related_to_regular_expressions
     http://docs.perl6.org/type/Match
     https://github.com/Util/Blue_Tiger/blob/master/translate_regex.pl



Thank you!

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to