Simple Regular Expression Help

Discussion in 'Perl Misc' started by vunet.us@gmail.com, May 16, 2007.

  1. Guest

    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+)\)/;
    , May 16, 2007
    #1
    1. Advertising

  2. 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
    Jens Thoms Toerring, May 16, 2007
    #2
    1. Advertising

  3. Greg Bacon Guest

    In article <>,
    <> 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
    Greg Bacon, May 16, 2007
    #3
  4. Mirco Wahab Guest

    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
    Mirco Wahab, May 16, 2007
    #4
  5. <> wrote in message
    news:...
    > 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
    Mario D'Alessio, May 16, 2007
    #5
  6. Guest

    On May 16, 10:51 am, 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)
    , May 17, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,287
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    844
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    11,948
    vforvikash
    Dec 29, 2008
  4. Joey
    Replies:
    7
    Views:
    464
    Kevin Spencer
    May 24, 2006
  5. Joe [at] subprint.com

    Simple Regular Expression Help

    Joe [at] subprint.com, Nov 24, 2008, in forum: Javascript
    Replies:
    8
    Views:
    129
    Joe [at] subprint.com
    Dec 2, 2008
Loading...

Share This Page