开发者

How to get values from within brackets?

I have a value Mac(user, name)

How do i use perl reg expression to do a check on values inside the brackets.

Value: Mac(user,name) Remove Mac.. and 开发者_开发知识库Do a check on (user,name) to check if it contains 'user'...?

Thanks for help!! :D


What about:

#!/usr/bin/perl
use strict;
use warnings;
use 5.10.1;

my $input = q!Mac(user, name)!;

my ($user, $name) = $input =~ /^.*?\((.+?),\s*(.+?)\)$/;

say "user=$user , name=$name";

output:

user=user , name=name


thanks all... i manage to use split to get data from my value

my @array = split (/[()]/, $input);

With this.. my value is store inside $array[1] :)


The split solution is very fragile, if the string is badly formatted, it will return unexpected results.

If you want to be more strict, you can use this :

my $input = 'Mac(user, name)';

if (my ($user, $name) = $input =~ / Mac \( ([^,]+?) , ([^,]+?) \) /x) {
    say "user: $user, name: $name";
} else {
    die "input string '$input' is wrongly formatted";
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜