Steffan Harris
Steffan Harris

Reputation: 9326

Regex parsing string substitution question

I would like to know if there is an easy way for parsing a string like this

set   PROMPT = Yes, Master?

What I would like to do, is parse one part of this string up to the equal sign and parse the second part after the equal sign into another string.

Upvotes: 1

Views: 802

Answers (3)

Andrew White
Andrew White

Reputation: 53496

Something like...

$phrase = 'set   PROMPT = Yes, Master?';
@parts = split /=/, $phrase;

or

($set, $value) = split /=/, $phrase, 2;

[updated] Changes per comments.

Upvotes: 6

daalbert
daalbert

Reputation: 1475

while ($subject =~ m/([^\s]+)\s*=\s*([^\$]+)/img) {
    # $1 = $2
}

Upvotes: 1

maerics
maerics

Reputation: 156434

Try matching this regex /\s*set\s*(\w+)\s*=\s*(.*)\s*$/ and setting the parts with $1 and $2:

my $str = 'set   PROMPT = Yes, Master?';
my ($k, $v) = ($1, $2) if $str =~ /\s*set\s*(\w+)\s*=\s*(.*)\s*$/;
print "OK: k=$k, v=$v\n"; OK: k=PROMPT, v=Yes, Master?

Upvotes: 2

Related Questions