Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regular expression to validate that string has only letters as per java definition.

Last post 07-30-2010, 6:32 PM by regrookie. 2 replies.
Sort Posts: Previous Next
  •  07-30-2010, 2:23 PM 70301

    Regular expression to validate that string has only letters as per java definition.

    Java doc says  A character is considered to be a letter if its general category type, provided by Character.getType(ch), is any of the following:

    • UPPERCASE_LETTER - General category "Lu" in the Unicode specification
    • LOWERCASE_LETTER - General category "Ll" in the Unicode specification
    • TITLECASE_LETTER - General category "Lt" in the Unicode specification.
    • MODIFIER_LETTER - General category "Lm" in the Unicode specification
    • OTHER_LETTER - General category "Lo" in the Unicode specification
    It is a performance bottle neck to go through all the characters in a for loop to validate the string. Did any of you guys manage to convert Character.isLetter() to regex? If yes can you share it?
     
    Thanks. 

     

  •  07-30-2010, 5:47 PM 70306 in reply to 70301

    Re: Regular expression to validate that string has only letters as per java definition.

    Raw Match Pattern:
    \p{L}

    Match Pattern Explanation:
    The regular expression:

    (?-imsx:\p{L})

    matches as follows:
      
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx:                 group, but do not capture (case-sensitive)
                             (with ^ and $ matching normally) (with . not
                             matching \n) (matching whitespace and #
                             normally):
    ----------------------------------------------------------------------
      \p{L}                    any character of: UTF macro 'L'
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------

    Java Code Example:

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    class Module1{
      public static void main(String[] asd){
      String sourcestring = "source string to match with pattern";
      Pattern re = Pattern.compile("\\p{L}");
      Matcher m = re.matcher(sourcestring);
      int mIdx = 0;
        while (m.find()){
          for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
            System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
          }
          mIdx++;
        }
      }
    }


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  07-30-2010, 6:32 PM 70307 in reply to 70306

    Re: Regular expression to validate that string has only letters as per java definition.

    Thanks Michael. Solved my issue.
View as RSS news feed in XML