Simple Regular Expression Help

V

vunet.us

How can I strip this line with regular expession to get 12345 number
within brackets:

$line = "some text is here (12345 ms)";

This did not work:

$text = $line;
$text =~ m/\((\d+)\)/;
 
J

Jens Thoms Toerring

How can I strip this line with regular expession to get 12345 number
within brackets:
$line = "some text is here (12345 ms)";
This did not work:
$text = $line;
$text =~ m/\((\d+)\)/;

No, beacuse the number isn't immediately followed by a closing
parenthesis but there's " ms" in between. If you always have
" ms" after the numbr just change your regexp to

$text =~ /\((\d+) ms\)/;

otherwise, if you want to accept arbitrary text after the number
(excluding the closing parenthesis), use

$text =~ /\((\d+)[^)]*\)/;
Regards. Jens
 
G

Greg Bacon

: How can I strip this line with regular expession to get 12345 number
: within brackets:
:
: $line = "some text is here (12345 ms)";
:
: This did not work:
:
: $text = $line;
: $text =~ m/\((\d+)\)/;

Try

if ($line =~ /\((\d+)[^)]+\)/) {
print "\$1 = '$1'\n";
}
else {
print "No match.\n";
}

Hope this helps,
Greg
 
M

Mirco Wahab

How can I strip this line with regular expession to
get 12345 number within brackets:

$line = "some text is here (12345 ms)";

This did not work:

$text = $line;
$text =~ m/\((\d+)\)/;

There are some misconceptions,

$text =~ /\((\d+)\)/;

This expression (m is superfluous here)
evaluates in "scalar context", so the (unused)
"return value" is the number of hits (would be
1 in this case if the regex is correct).

If you say BRACKET '(' NUMBER (\d+) BRACKET ')',
the Regex engine thinks you mean it, so if
there is something between NUMBER and BRACKET,
your expression would not match. Better catch
such cases by an expression like:

$text =~ /\( \D* (\d+) \D* \)/x

\D* means 'no number thingy', zero or more times,
/x means 'use formatting' and ignore whitespace
in Regex.

You could do several things now:

- 'in place' replacement of the whole bracket (...)
thing by the number it contains (in $1)

$text =~ s/\( \D* (\d+) \D* \)/$1/x;

which will result in: some text is here 12345
note the s/ (substitution)

- *extract* the number but don't care about the text

my @number = $text =~ /\( \D* (\d+) \D* \)/x;

after this, the array @number has one element: 1234
(here, the expression is evaluated in list context,
returning the matches then)


Of course, there are more variants, but to give
you a start, this should suffice.

Regards

Mirco
 
M

Mario D'Alessio

How can I strip this line with regular expession to get 12345 number
within brackets:

$line = "some text is here (12345 ms)";

This did not work:

$text = $line;
$text =~ m/\((\d+)\)/;

You didn't account for the "ms" in the text string:

$text =~ m/\((\d+) ms\)/;

or if there can be other strings other than "ms":

$text =~ m/\((\d+).*\)/;

Mario
 
U

usenet

$line = "some text is here (12345 ms)";

This did not work:

$text = $line;
$text =~ m/\((\d+)\)/;

You are telling Perl to match an open paren, followed by one or more
digits, followed by a close paren. but your string doesn't fit that
criteria (it has digits followed by a whitespace and two alphas).

You could do something like this (and consolidate the command), which
will (greedily) match everything after the digits up to the (last)
closing paren in your input string:

my ($numbers) = ($line =~ /\((\d+).*\)/);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top