5 Replies Latest reply: Dec 12, 2012 7:21 AM by 939520 RSS

    Regex Pattern matching

    Emily Robertson
      Hi there, sorry to bother, i been having problem with regex expression macthing.

      String criteria = "Mary-Anna";
      String regexWord = "((\\w+)[^A-Z](\\w+))";
      Pattern patternWord = Pattern.compile(regexWord);
      Matcher matcherWord = patternWord.matcher(criteria);
      if (matcherWord.find()) {
      System.out.println(matcherWord.group(1));
      } else {
      System.out.println("nomatch");
      }

      this return Mary-Anna which is what i needed

      String criteria = "Mary' 'Anna"; // 2 single quotes
      String regexWord = "((\\w+)[^A-Z](\\w+))";
      Pattern patternWord = Pattern.compile(regexWord);
      Matcher matcherWord = patternWord.matcher(criteria);
      if (matcherWord.find()) {
      System.out.println(matcherWord.group(1));
      } else {
      System.out.println("nomatch");
      }

      But am not sure why if i put in 2 single qoute and it return only Mary ??

      am trying to get the pattern to match a [name]+[non a-z chars]+[name]

      thanks
        • 1. Re: Regex Pattern matching
          TPD-Opitz
          Please redt this to learn how to format your code examples:
          https://wikis.oracle.com/display/Forums/Forums+FAQ
          -----
          Emily Robertson wrote:
          But am not sure why if i put in 2 single qoute and it return only Mary ??
          Beause the single quote is not covered by <tt>\w</tt>
          http://docs.oracle.com/javase/7/docs/api/
          am trying to get the pattern to match a [name]+[non a-z chars]+[name]
          You have a missunderstanding of what characters a name can consist of. If you expect your names beeing sourrounded by single quotes (wich would be a rather unusual requirement) you have to enhance your regular expression.
          Be carefull when using the <tt>?</tt> wildcard, beause this will not ensure single quotes on both sides!

          bye
          TPD
          • 2. Re: Regex Pattern matching
            DrClap
            TPD Opitz-Consulting com wrote:
            You have a missunderstanding of what characters a name can consist of.
            Yes. Read this blog entry: http://joconner.com/2012/12/no-apostrophes/ because he's talking to YOU.
            • 3. Re: Regex Pattern matching
              TPD-Opitz
              Yes. Read this blog entry: http://joconner.com/2012/12/no-apostrophes/ because he's talking to YOU.
              Edited by: TPD Opitz-Consulting com on 12.12.2012 14:03
              (previous content removed)

              Sorry, did not reread the OP and relied on my memory.
              At the end the regular expression in the OP is simply wrong.

              bye
              TPD
              • 4. Re: Regex Pattern matching
                gimbal2
                DrClap wrote:
                TPD Opitz-Consulting com wrote:
                You have a missunderstanding of what characters a name can consist of.
                Yes. Read this blog entry: http://joconner.com/2012/12/no-apostrophes/ because he's talking to YOU.
                These kind of blogs are really nice, it puts things into perspective and refreshes that which you so easily repress. It all boils down to that thing people don't like to do: you have to think about it. Deeply.
                • 5. Re: Regex Pattern matching
                  939520
                  On a related note, a first or last name my have a single quote in it. Example: John O'Connell. Your program should treat the last name as one word.
                  You will have to deal with that situation too.