String Matching with Parentheses

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karlectomy
    New Member
    • Sep 2007
    • 64

    String Matching with Parentheses

    Hi all,

    I am parsing text from a text file and I use Pattern and Matcher for the majority of the matching. My problem comes when I use the String.matches( String) function. Some of the lines have unclosed parentheses or unopened parentheses and These will throw the regex.PatternSy ntaxException ... some sort of exception. I have handled these but I was wondering if there was a way to ignore the parentheses or if I have to just handle the errors as they pop up?

    An example of the function how I use it:
    [Code="JAVA"]
    String tester = "ralph";
    String testee = "ralph)";

    if(tester.match es(testee)){
    //Do SOmething Brilliant
    }
    [/Code]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by karlectomy
    Hi all,

    I am parsing text from a text file and I use Pattern and Matcher for the majority of the matching. My problem comes when I use the String.matches( String) function. Some of the lines have unclosed parentheses or unopened parentheses and These will throw the regex.PatternSy ntaxException ... some sort of exception. I have handled these but I was wondering if there was a way to ignore the parentheses or if I have to just handle the errors as they pop up?

    An example of the function how I use it:
    [Code="JAVA"]
    String tester = "ralph";
    String testee = "ralph)";

    if(tester.match es(testee)){
    //Do SOmething Brilliant
    }
    [/Code]
    Have a look at the the specs for the String class and see if there is no methods that can achieve what you want without the fuss of regex e.g regionMatches.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Originally posted by karlectomy
      An example of the function how I use it:
      [Code="JAVA"]
      String tester = "ralph";
      String testee = "ralph)";

      if(tester.match es(testee)){
      //Do SOmething Brilliant
      }
      [/Code]
      You need to describe your problem better. Exactly what sort of regex patterns are you using? This example looks like it could be solved by just using equals, but I'll wait for your explanation. Where are your "regex pattern strings" coming from?

      Comment

      • karlectomy
        New Member
        • Sep 2007
        • 64

        #4
        Thank you for the insight. I used String.equals(S tring) instead and it seems to work just fine.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by karlectomy
          Thank you for the insight. I used String.equals(S tring) instead and it seems to work just fine.
          Cheers! Testing for string equality using regular expressions is overkill, to say the least.

          Comment

          Working...