Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Regex issues

by mrras25 (Acolyte)
on Jul 12, 2013 at 21:48 UTC ( #1044087=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Regex issues
Select or Download Code
Re: Regex issues
by choroba (Abbot) 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 (Bishop) 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.

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

      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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1044087]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2014-09-02 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls