Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple Regular Expression Help

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 ___ (E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      05-16-2007
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      05-16-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Mario D'Alessio
Guest
Posts: n/a
 
      05-16-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      05-16-2007
On May 16, 10:51 am, (E-Mail Removed) 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
Simple Regular Expression Help Joe [at] subprint.com Javascript 8 12-02-2008 09:27 PM
Need Help With Simple Regular Expression Joey ASP .Net 7 05-24-2006 02:41 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments