New to Regex, Can't figure out conditional ?()| method

已锁定 New to Regex, Can't figure out conditional ?()| method

  • Thursday, March 01, 2012 2:47 PM
     
     

    Ok, I've used some regular expressions in the past but I've never gotten deep into them. I thought I would expand my knowledge but I'm hitting three different walls. First How do I use the Conditional Operator ?() and ?()|

    I needed to use it on a time format in a flat text file

    ie.

    10:38 A

    I extracted the time and tried to flip the A to an AM or P to an PM with

    ?(A)AM|PM

    what got me is that it did still find the A and P but didn't put them out as AM or PM.

    Second one

    I'm trying to break down the parts of the following lines

    (1)   CL STAR ZONE                      59      (6)

    (?<Position>\(\d*\))\s*(?<Name>\s+[A-Z\s]+)\s*(?<Weight>([0-9]{1,2}))\s*(?<Unknown1>\(\d*\))

    or

    (3)   LEONARD SLYE                      81      (5/2)

    (?<Position>\(\d*\))\s*(?<Name>\s+[A-Z\s]+)\s*(?<Weight>([0-9]{1,2}))\s*(?<Unknown1>\(\d*/\d*\))

    I can do either but not both. I've got over 20 years in i.t. and 17 plus in microsoft c languages but I just haven't really dealt with this area much.

    I can't remember the third one

    Thanks,

    Bubba

All Replies

  • Thursday, March 01, 2012 6:49 PM
     
     Answered

    1st one can't be done with a regex. Conditional expressions work *inside* the matching process, but do nothing with the replacement or changing process. You can use \d{1,2}:\d{2} (A|P)

    And then in the Replace Method use a MatchEvaluator and do the actual swapping in code, not with a regex

    2nd one. 

    change the last bit to read: \(\d*(/\d*)?\))

    To make the division an optional part.


    My blog: blog.jessehouwing.nl