Naghaveer R
Naghaveer R

Reputation: 2944

Need help in pattern matching?

CODE:
    $str="Feature=Legacy--->dns3.1";
    @fea=split/[=-->]/,$str;
    print $_ foreach(@fea);

ACTUAL OUTPUT: Feature
        Legacy
        dns3.1

But If any character in the value for example

CODE:
    $str="Feature=Legacy-system--->dns3.1";
    @fea=split/[=-->]/,$str;
    print $_ foreach(@fea);

OUTPUT: Feature
        Legacy
        System
        dns3.1

But I want output as Feature Legacy-system dns3.1

please help me how to resolve it?

Upvotes: 0

Views: 47

Answers (2)

alexmac
alexmac

Reputation: 239

The idea of the multi part split is interesting, the match is

@fea = split /=|-{1,}>/, $str;

The match change I would suggest is also the '-' character, change to '-{1,}' this will match any number of '-' characters from 1 to n.

Side comments, readability is often an issue for coders, so more spaces and the foreach loop is tight and nice but for others to come to your code later makes it tough if they are not super perl programmers.

I have never though of using split this way, I like it, seems so obvious now so even old perl dogs can learn new tricks, so I thank you.

Upvotes: 0

SzG
SzG

Reputation: 12619

You defined the splitting regexp as a SINGLE character, which is either =, - or >. But you want to split at = or --->.

@fea = split /=|--->/, $str;

Upvotes: 1

Related Questions