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. Advertisements

  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. Advertisements

  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. Advertisements

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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    957
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    2,030
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    786
  4. Xah Lee
    Replies:
    1
    Views:
    1,181
    Ilias Lazaridis
    Sep 22, 2006
  5. Xah Lee
    Replies:
    8
    Views:
    691
    Ilias Lazaridis
    Sep 26, 2006
  6. Replies:
    3
    Views:
    1,110
    Reedick, Andrew
    Jul 1, 2008
  7. RolfK
    Replies:
    1
    Views:
    2,182
    Martin Honnen
    Jun 7, 2009
  8. Roger Pack

    RCR: regex + regex

    Roger Pack, Oct 26, 2009, in forum: Ruby
    Replies:
    3
    Views:
    166
    Robert Klemme
    Oct 27, 2009
Loading...