#1
  1. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Sep 2007
    Location
    outside Washington DC
    Posts
    2,642
    Rep Power
    3699

    Can you use the number of matched characters later in the regex?


    I'm using Java's regex engine. I've got data that matches:

    (\\d{8}+|\\p{Upper}{2}\\d{6}+|\\p{Upper}\\d{7}+)
    What it really means is any of:
    eight digits
    or two letters and six digits
    or one letter and seven digits.

    It would be cool if I could have some sort of backreference to the number of characters matched in the pattern so I could use something like

    \\p{Upper}{0,2}\\d{8-n}+
    Is this possible?
    Thanks
    Pat
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,068
    Rep Power
    9398
    Not really, but if you can have
    Code:
    (?=[\d\p{Upper}]{8})(\p{Upper}{0,2}\d{6,8})
    That's normally more complicated than I go - I typically go the same kind of alternation route you did:
    Code:
    (dd|U(d|U))d{6}
    (dd|Ud|UU)d{6}
    d{8}|Ud{7}|UUd{6}
    (Basically, I start with the first and unravel as I feel like it.)

    Comments on this post

    • fishtoprecords agrees

IMN logo majestic logo threadwatch logo seochat tools logo