1 2 Previous Next 23 Replies Latest reply: Sep 28, 2006 7:36 AM by 807569 RSS

    More regex help: Word once or not at all

    807569
      Hi - and here's hoping someone is as helpful as sabre150 was last time. I have a String which if of the form ABC.D [CFD1234] or the form ABC.D [1234] (that is, the CFD may or may not be there).

      So I would like to pull out the numbers 1234 from either of these Strings: If I just take the first String the pattern is
      "\\[CFD(\\d+)\\]"
      and I tried the following for "CFD" once, or not at all but it didn't work
      "\\[CFD?(\\d+)\\]"
      This didn't work because it reads "CF" followed possibly by "D". So how do I say The word "CFD" once or not at all?

      Cheers for any help

      PS. I've got this working using the regex
      "\\[CFD(\\d+)\\]|\\[(\\d+)\\]"
      and then matching on group #2 if there's no match on group #1 but this seems a bit ugly to me!
        1 2 Previous Next