Velocity Reviews > Perl > Simple Regular Expression Help

# Simple Regular Expression Help

vunet.us@gmail.com
Guest
Posts: n/a

 05-16-2007
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+)\)/;

Jens Thoms Toerring
Guest
Posts: n/a

 05-16-2007
wrote:
> 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

--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de

Greg Bacon
Guest
Posts: n/a

 05-16-2007
In article < .com>,
<> wrote:

: 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
--
As against a Jesus, the historic choice of the mass-man goes regularly
to some Barabbas.
-- Albert Jay Nock on democracy

Mirco Wahab
Guest
Posts: n/a

 05-16-2007
wrote:
> 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

Mario D'Alessio
Guest
Posts: n/a

 05-16-2007

<> wrote in message
news: oups.com...
> 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

usenet@DavidFilmer.com
Guest
Posts: n/a

 05-16-2007
On May 16, 10:51 am, vunet...@gmail.com wrote:
> \$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+).*\)/);

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)