开发者

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 thi开发者_开发技巧s string up to the equal sign and parse the second part after the equal sign into another string.


Something like...

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

or

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

[updated] Changes per comments.


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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜