I have a string in Perl like: "Full Name (userid)
" and I want to return just the userid (everything between the "()
"'s).
What regular expression would do this in Perl?
I have a string in Perl like: "Full Name (userid)
" and I want to return just the userid (everything between the "()
"'s).
What regular expression would do this in Perl?
This will match any word (\w
) character inside of "(
" and ")
"
\w
matches a word character (alphanumeric or _), not just [0-9a-zA-Z_]
but also digits and characters from non-roman scripts.
my($username) = $str =~ /\((\w+)\)/;
# or
$str =~ /\((\w+)\)/;
my $username = $1;
If you need it in a s///
, you can get at the variable with $1
or \1
.
$str =~ s/\((\w+)\)/$1:\1/; # pointless example
If you want to capture all possibilities these would work better:
my($username) = $str =~ /\(([^\)]+)\)/;
# or
my($username) = $str =~ /\((.+?)\)/;
If your regexp starts to get complicated, I would recommend you learn about the /x
option.
my($username) = $str =~ / \( ( [^\)]+ ) \) /x;
perldoc perlre
, for more information.
If you are just beginning to learn regexps, I would recommend reading perldoc perlretut
.
Escape the brackets, capture the string in-between. Assuming user ids consist of \w
characters only:
my ($userid) = $str =~ /\((\w+)\)/ ;
m//
in list context returns the captured matches.
More information on capturing can be found in
C:\>
perldoc perlretut
This will get anything between the parentheses and not just alphanumeric and _. This may not be an issue, but \w will not get usernames with dashes, pound signs, etc.
$str =~ /\((.*?)\)/ ;
When you search for something between brackets, e.g. '< > [ ] ( ) { }' or more sophisticated such as xml/html tags, it's always better to construct your pattern in the way:
opening bracket, something which is NOT closing bracket, closing bracket
Of course, in your case 'closing bracket' can be omitted:
my $str = 'Full Name (userid)';
my ($user_id) = $str =~ /\(([^\)]+)/;
In addition to what has been said: If you happen to know that your string has exactly this format, you can also do without regexp. If your string is in $s
, you could do
chop $s; # throws away last character (by assumption must be closing parenthesis)
$username=substr($s, rindex($s,'(') + 1);
As for the regexp solutions, can you be sure that the full name can not contain also a pair of parentheses? In this case, it might make sense anchoring the closing ')' at the end of the pattern:
/ [(] # open paren
([^(]+) # at least one non-open paren
[)] # closing paren
$ # end of line/pattern
/x && $username = $1;