in reply to regex pattern matches

Hi,

You are not grouping the REDIRECTURL element value in if statement, instead grouping only the REDIRECTURL tag. So $1 will have the value as REDIRECTURL U need to change the if statement as

if ($data =~ m|<REDIRECTURL>(.*)</REDIRECTURL>| ) { print "Found: $1\n"; }

Update:

As suggested by johngg modified the if statement.


All is well

Replies are listed 'Best First'.
Re^2: regex pattern matches
by johngg (Canon) on Aug 22, 2014 at 12:48 UTC
    The angle brackets ('<' & '>') are not regex metacharacters so need not be escaped. Choosing a different pattern delimiter would mean that nothing needs to be, making the pattern a little easier on the eye.

    $ perl -E ' $text = q{asd<TAG>dsyuhf</TAG>urrh}; $item = $1 if $text =~ m{<TAG>([^<]+)</TAG>}; say $item;' dsyuhf $

    Note also that I capture ([^<]+) because (.*) is greedy.

    $ perl -E ' $text = q{asd<TAG>dsyuhf</TAG>urrh<TAG>dsvnnhf</TAG>urubb}; $item = $1 if $text =~ m{<TAG>(.*)</TAG>}; say $item;' dsyuhf</TAG>urrh<TAG>dsvnnhf $

    I hope this is useful.

    Cheers,

    JohnGG

Re^2: regex pattern matches
by Laurent_R (Canon) on Aug 22, 2014 at 18:48 UTC
    johngg has already mentioned the possible issue, but it seems you did not notice. I would suggest that it would be safer to use a non-greedy quantifier:
    if ($data =~ m|<REDIRECTURL>(.*?)</REDIRECTURL>| ) { print "Found: $1\n"; }
    just in case there are two <REDIRECTURL>...</REDIRECTURL> pairs of tags (assuming this is possible).
Re^2: regex pattern matches
by argos (Initiate) on Aug 22, 2014 at 12:32 UTC

    I see your logic, that looks better than what I came up with as it clearly defines the start and end pattern match. I really want whatever is found between those two points which you capture with parenthesis. Thanks!