helping with regex in java.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndedhia1
    New Member
    • Jan 2009
    • 112

    helping with regex in java.

    If I have a line that contains this: ProdBC02TradeSe rver24
    and I want to break it up into BC02 and 24, how would I go about doing that.

    Right now,
    I am doing this: Prod(BC\\d+)\\S +(\\d+)
    But that only breaks it up into:
    BC02 and 4. I need 24, not just the 4.
    Can someone show me what I am doing wrong

    Thanks
    Nirav
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    The problem is that the regex picks the shortest match, eg the match with only 1 digit. I suggest adding the next character, whether that be space, end of line, eof, or other.

    What class are you using to execute the regexp?

    Comment

    • ndedhia1
      New Member
      • Jan 2009
      • 112

      #3
      I am importing these two lines:
      [code=java]
      import java.util.regex .Matcher;
      import java.util.regex .Pattern;
      [/code]

      I am defining the regex expressing in a variable here:
      [code=java]
      public static String TSAssingmentFil ter = "Prod(BC\\d+)\\ S+(\\d+)\\s+(\\ w+)\\s+\\S+\\s+ (\\d+)\\s+(\\S* )\\s+(\\d+)";
      [/code]

      I am using this function to use the regex expression on the specific line:
      [code=java]
      if(OutlierUtili ties.getInstanc e().match(line, TSAssingmentFil ter))
      [/code]

      I am using this function to grep out what i am looking for:
      [code=java]
      public boolean match(String line,String regExp)
      {
      pattern = Pattern.compile (regExp);
      matcher = pattern.matcher (line);
      return matcher.find();
      }
      [/code]

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Insert a non digit character afterwards: \D or [^\d], and change the \\s+ to a \\s?

        Comment

        • ndedhia1
          New Member
          • Jan 2009
          • 112

          #5
          so instead of this: Prod(BC\\d+)\\S +(\\d+)

          are you saying I should try this: Prod(BC\\d+)\\S \\D(\\d+)

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            "Prod(BC\\d+)\\ S+(\\d+)\\s+ ..."to
            "Prod(BC\\d+)\\ S+(\\d+)[^\\d]\\s?..."

            Comment

            • ndedhia1
              New Member
              • Jan 2009
              • 112

              #7
              Im sorry...im having trouble in this part:
              Prod(BC\\d+)\\S+(\\d+)
              But that only breaks it up into:
              BC02 and 4. I need 24, not just the 4.

              So i changed it to this and it worked:
              [code=java]
              public static String TSAssingmentFil ter = "Prod(BC\\d +)\\S+\\D(\\d+)\\s+(\\w+ )\\s+\\S+\\s+(\ \d+)\\s+(\\S*)\ \s+(\\d+)";
              [/code]

              THANKS A LOT FOR YOUR HELP!!!

              Comment

              Working...