Regex Precedence

Discussion in 'Perl Misc' started by Kevin Nechodom, Nov 1, 2005.

  1. I am trying to format some text in the midst of a s/// substitution. I have a field that can be:
    (blank)
    char
    char-num, char - num, char#num
    num
    num-num, num - num, num#num

    and I want to reformat it to:
    (char or num)-num

    So I'm trying to match it with:
    s/(\w*)(?: |-|\#)*(\w*)/$1-$2/

    But when I come to the case of '632', it returns '-632' instead of '632-'. How do I change this to return '632-'? I dont' understand regex precedence here.

    Thanks,

    --
    Kevin Nechodom
    University Health Care
    Kevin dit Nechodom ack hsc dit utah dit edu
    "Call me paranoid, but I think you are reading what I'm writing!"
    Kevin Nechodom, Nov 1, 2005
    #1
    1. Advertising

  2. "Kevin Nechodom" <> wrote in
    news:dk6c0q$sc6$:

    > <HTML><HEAD>
    > <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
    > <META content="MSHTML 6.00.2900.2769" name=GENERATOR></HEAD>


    Please don't.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 1, 2005
    #2
    1. Advertising

  3. Kevin Nechodom

    James Guest

    Check if you have a space before 632.

    James
    James, Nov 1, 2005
    #3
  4. Kevin Nechodom wrote:
    > I am trying to format some text in the midst of a s/// substitution. I have a
    > field that can be:
    > (blank)
    > char
    > char-num, char - num, char#num
    > num
    > num-num, num - num, num#num
    >
    > and I want to reformat it to:
    > (char or num)-num
    >
    > So I'm trying to match it with:
    > s/(\w*)(?: |-|\#)*(\w*)/$1-$2/
    >
    > But when I come to the case of '632', it returns '-632' instead of '632-'. How
    > do I change this to return '632-'? I dont' understand regex precedence here.


    This may be what you want:

    s/(\w+)(?: *[#-] *)?(\d*)/$1-$2/g;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Nov 1, 2005
    #4
  5. Kevin Nechodom

    Matt Garrish Guest

    "Kevin Nechodom" <> wrote in message
    news:dk6c0q$sc6$...
    > I am trying to format some text in the midst of a s/// substitution. I
    > have a field that can be:
    > (blank)
    > char
    > char-num, char - num, char#num
    > num
    > num-num, num - num, num#num


    > and I want to reformat it to:
    > (char or num)-num


    > So I'm trying to match it with:
    > s/(\w*)(?: |-|\#)*(\w*)/$1-$2/


    > But when I come to the case of '632', it returns '-632' instead of '632-'.
    > How do I change
    > this to return '632-'? I dont' understand regex precedence here.


    Please do not post html messages to usenet, and vcf files are equally
    unwanted.

    My guess is that it has to do with making the content inside your first
    capture optional (you might want to consider reading Mastering Regular
    Expressions). It doesn't have to match, which means that your look-ahead
    assertion can, and if it is followed by any \w characters those characters
    will come out after the dash in your replacement. For example:

    $num = ' -#632';
    $num =~ s/(\w*)(?: |-|\#)*(\w*)/$1-$2/;
    print $num;

    Without seeing any of the actual data you're working with, however, I'm not
    going to speculate at what will or will not fix your problem.

    Matt
    Matt Garrish, Nov 1, 2005
    #5
  6. >>> Matt Garrish<> 10/31/2005
    6:43 PM >>>

    "Kevin Nechodom" <> wrote
    in message
    news:dk6c0q$sc6$...
    >> So I'm trying to match it with:
    >> s/(\w*)(?: |-|\#)*(\w*)/$1-$2/


    >> But when I come to the case of '632', it returns '-632'

    instead of '632-'.
    >> How do I change
    >> this to return '632-'? I dont' understand regex

    precedence here.

    >Please do not post html messages to usenet, and vcf files

    are equally
    >unwanted.

    I'll do my best, but I won't see it (HTML) Please let me
    know if the problem persists.

    >Without seeing any of the actual data you're working with,

    however, I'm not
    >going to speculate at what will or will not fix your

    problem.
    My bad! Text was being interpreted, so rhat '632-' was
    veing displayed -632.

    >Matt


    Thanks,

    --
    Kevin Nechodom
    University of Utah Hospital and Clinics
    Kevin dit Nechodom ack hsc dit utah dit edu
    "Call me paranoid, but I think you are reading what I'm
    writing!"
    Kevin Nechodom, Nov 1, 2005
    #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. David Frauzel

    Operator precedence

    David Frauzel, May 13, 2004, in forum: Perl
    Replies:
    2
    Views:
    529
    Luc Van Hove
    May 17, 2004
  2. Replies:
    1
    Views:
    8,772
    Siva M
    Sep 15, 2005
  3. kbd

    Operator Precedence

    kbd, Jul 22, 2004, in forum: Java
    Replies:
    10
    Views:
    734
    Roedy Green
    Jul 27, 2004
  4. Replies:
    3
    Views:
    401
    Roedy Green
    Sep 13, 2005
  5. Replies:
    3
    Views:
    731
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page