in reply to Re: Array of strings search
in thread Array of strings search

Ok, got it so far, but i cant get to split the sender part from it. I tried:

( $sender ) = split (/\s(sender=(.*)@(.*),)/);

But just get cut the part after the "," and \s away, like:

client_address=185.15.51.41, EMPTYFROMHERE

What am i missing? Online Regex tools tell me that all is great.

Replies are listed 'Best First'.
Re^3: Array of strings search
by Laurent_R (Canon) on Nov 24, 2017 at 00:09 UTC
    You obviously don't really understand split. Follow the link and look it up.

    split is aimed at splitting a string into a list of parts, in order to later use these parts. Usually, the first parameter of split is the separator on which you want to break up the string (although the separator can be a bit more complicated than a simple character). You could certainly use split on your problem (as a part of the solution), it is probably not the best tool for your task. A simple regex is probably better.

    One possible way to get the sender:

    my $sender = $1 if /sender=(\S+)/;
    This captures a string of as many characters other than spaces as possible following the "sender=" string in the $_ variable.

    Update: I have never actually seen any problem with this syntax, but I have read several times and AnomalousMonk reminds me that conditionally defining lexicals can lead to unexpected results. Perhaps it is better to write:

    my $sender; $sender = $1 if /sender=(\S+)/; if (defined $sender) { # ...
    or possibly:
    if (/sender=(\S+)/) { my $sender = $1; do_something_with_sender(); }
    Thanks, AnomalousMonk.
Re^3: Array of strings search
by AnomalousMonk (Archbishop) on Nov 23, 2017 at 22:32 UTC
    I tried: ...

    Exactly what code did you try? On exactly what data? Please see Short, Self-Contained, Correct Example.

    ( $sender ) = split (/\s(sender=(.*)@(.*),)/);

    This doesn't look like a sensible split invocation. Have you read and understood the split documentation?

    client_address=185.15.51.41, EMPTYFROMHERE

    I don't understand where this extracted substring comes from; the substring  EMPTYFROMHERE appears nowhere in the OPed data. Again, Short, Self-Contained, Correct Example. Please help us to help you.

    Online Regex tools tell me that all is great.

    Which online regex tools? How does their advice apply to the operation of split?


    Give a man a fish:  <%-{-{-{-<