On 10/24/21 19:09, Ralph Mellor wrote:
Wish I had a Q[;;;] expression inside a regex

You can put any Raku code that produces a string
into a regex by using the syntax `$(code)`.

So this displays `True`:

```
say so Q[ <foo></foo> / ;;; ^&%%$$ ] ~~ / $( Q[ <foo></foo> / ;;; ^&%%$$ ] ) /
```
as does:
```
my $string = Q[ <foo></foo> / ;;; ^&%%$$ ];
say so $string ~~ / $( $string ) /;
```

Interesting!

Any way to get the reverse to work with Q[]?

> my Str $x = Q[abc;";def];
abc;";def

> my $x ~~ s/ Q[;";] //;
===SORRY!===
Unrecognized regex metacharacter ; (must be quoted to match literally)
------> my $x ~~ s/ Q[⏏;";] //;
Unrecognized regex metacharacter " (must be quoted to match literally)
------> my $x ~~ s/ Q[;⏏";] //;
Unable to parse expression in metachar:sym<[ ]>; couldn't find final ']' (corresponding starter was at line 1)
------> my $x ~~ s/ Q[;"⏏;] //;

------> my $x ~~ s/ Q[;⏏;] //;
    expecting any of:
        infix stopper


when does regex NOT start reading at the
beginning of the input data stream?

Never. It *always* starts at the beginning of the input.

I thought so!


Why does it need the ^

You use `^` when you want a pattern to be *anchored*
to the start, so that if it fails at the start then that's it.

Oh I get it now.  If the pattern does not occur exactly at
the anchor, it does not matter where else the pattern
occurs.  Nice tool for the tool chest!

I was thinking the careening through the data stream
had to start at the beginning or ending and that
did to make any sense to me.

Careening through web pages, sadly, nothing is ever
at the beginning or end.  And they often times mix
UTF8 and UTF16 characters


To understand this, consider an example.

Let's say you have an input string 'foo'.

An `/ oo /` regex will match, because it doesn't contain an
anchor, so the pattern is free to match *anywhere* in the
input string. That is, while it *fails* to match *at the start*,
because an 'o' does *not* match an 'f', *it keeps going*,
and then succeeds when *the next* two input characters
*do* match.

In contrast, `/ ^oo /` will *not* match, because the `^`
"anchors" the match to the *start* of the input string, i.e.
immediately before the 'f' in 'foo', and the first 'o' in `^oo`
does *not* match the 'f', and given that the match is
*anchored* to the start, if it fails at the start, it gives up.

----

I've no idea what was causing your hang, but fwiw it
doesn't make sense to me that it happened due to a
zero length input or an undefined variable.

----

love, raiph


Great tutorial.  Thank you!  (I love you too Raiph.)

Reply via email to