> On Oct 23, 2021, at 7:48 PM, ToddAndMargo via perl6-users
> <perl6-us...@perl.org> wrote:
>
> On 10/23/21 17:37, Bruce Gray wrote:
>>> On Oct 23, 2021, at 6:43 PM, ToddAndMargo via perl6-users
>>> <perl6-us...@perl.org> wrote:
>>>
>>> Hi All,
>>>
>>> Wish I had a Q[;;;] expression inside a regex, but I don't
>>>
>>> This is my notes on how to do a regex with a special
>>> characters in it:
>>>
>>> Regex with literals in it:
>>> $JsonAddr ~~ s| (';') .* ||;
>>>
>>> It "usually" works.
>>>
>>>
>>> Unfortunately this one hangs my program (I am slicing
>>> up a web page):
>>>
>>> $NewRev ~~ s/ .*? ('Release Notes <strong>V') //;
>>>
>>> I need a better way of doing the above.
>>>
>>> Many thanks,
>>> -T
>>>
>>>
>> Just anchor the start of the pattern, using `^` :
>> $NewRev ~~ s/ ^ .*? ('Release Notes <strong>V') //;
>> In the code below:
>> * Target_V is matched by the original pattern, at high speed.
>> * Target_V is matched by the anchored pattern, at high speed.
>> * Target_Z hangs the original pattern.
>> * Target_Z is correctly fails at match the anchored pattern, at high
>> speed.
>> my $target_V = 'Release Notes <strong>V'; # Will match pattern(s)
>> below
>> my $target_Z = 'Release Notes <strong>Z'; # Will not match pattern(s)
>> below
>> # Uncomment one of these two lines:
>> my $target = $target_V;
>> # my $target = $target_Z;
>> # Simulate a big HTML page:
>> my $NewRev = ('abcdefghijklmnopqrstuvwxyz' x 100) ~ $target
>> ~ ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' x 100) ~ $target
>> ~ ('12345678901234567890123456' x 100);
>> # Uncomment one of these two lines:
>> $NewRev ~~ s/ .*? ('Release Notes <strong>V') //; # Original
>> # $NewRev ~~ s/ ^ .*? ('Release Notes <strong>V') //; # Anchored
>
>
> Hi Bruce,
>
> I did fix the hang. I was accidentally feeding the
> regex a nul string.
>
> I am not understanding what you mean by "anchor"
>
> -T
From https://docs.raku.org/language/regexes#Anchors :
Regexes search an entire string for matches.
Sometimes this is not what you want.
Anchors match only at certain positions in the string,
thereby anchoring the regex match to that position.
The ^ anchor only matches at the start of the string.
The $ anchor only matches at the end of the string.
Code examples are given on that web page,
but I (and, I am sure, other readers) can expound on anchors
on request if the examples fail to suffice.
As to your "null string", I am glad that you resolved your problem, but I
cannot get this code to hang:
$NewRev ~~ s/ ^ .*? ('Release Notes <strong>V') //;
, just by preceding it with this line:
$NewRev = '';
, so I may misunderstand the nature of your accident.