http://www.perlmonks.org?node_id=1044087

mrras25 has asked for the wisdom of the Perl Monks concerning the following question:

I have returned values like so:

rtxecx38006_1439 wtxecx380d1_1927 wtxecx38009_0028 ccaecx34000_4430 lcaecx38000_1453 kmohuspvmwo@171.197.27.110 rtxecx38005_2604 wtxecx380d0_0057

And what I need is for it to be split on the special characters if special characters exist. So that the end results look like so:

rtxecx38006 wtxecx380d1 wtxecx38009 ccaecx34000 lcaecx38000 kmohuspvmwo rtxecx38005 wtxecx380d0

Replies are listed 'Best First'.
Re: Regex issues
by choroba (Cardinal) on Jul 12, 2013 at 21:52 UTC
    my @values = qw( rtxecx38006_1439 wtxecx380d1_1927 wtxecx38009_0028 ccaecx34000_4430 lcaecx38000_1453 kmohuspvmwo@171.197.27.110 rtxecx38005_2604 wtxecx380d0_0057 ); s/[_@].*//, print "$_\n" for @values;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regex issues
by ww (Archbishop) on Jul 12, 2013 at 23:10 UTC

    I think (and hope) choroba has hit your definition of "special characters" on the head.... even if it is a specification that is utterly non-standard, arcane, and imprecise.

    You should have provided that specification because "special characters" has special meanings in certain contexts -- and because the solution offered keeps some numbers and deletes others... in conformity with your example. But defining a spec based on a small example/sample is a risky and often ambiguous process.

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.
Re: Regex issues
by TomDLux (Vicar) on Jul 13, 2013 at 05:16 UTC

    I dislike using regexes when simple string functions can handle the job.

    By extracting the magic delimiter characters into a predefined constant, the code is clearer and more easily extendable. split will partition an input string on any of those special characters, and the wrapper (xx)[0] extracts the first element from the resulting list.

    my @DELIMS = (qw( _ @ )); for my $elem ( @inputs ) { push @result, ( split /[@DELIM]/, $elem )[0]; }

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      my @DELIMS = (qw( _ @ )); ... ... /[@DELIM]/ ...

      If the default value of  $" is unchanged, the array  @DELIMS will be interpolated into the regex character class with an extraneous space character, which is then a valid part of the class: a bug waiting to happen. Locally set  $" to the empty string or, better yet, use join to form the char class string.

      >perl -wMstrict -le "my @DELIMS = (qw( _ @ )); ;; my $rx = qr{[@DELIMS]}; print $rx; ;; { local $\" = ''; $rx = qr{[@DELIMS]}; } print $rx; ;; my $cc = join '', @DELIMS; $rx = qr{[$cc]}; print $rx; " (?^:[_ @]) (?^:[_@]) (?^:[_@])

      Do not multiply bugs without necessity!

      ... I dislike using regexes..
      But your usage of "split" ... split /[@DELIM]/, $elem .. is still regexes...
      Or you say otherwise?