Cyrillic Check

Today I came across an interesting drawback: I have deployed my web application in Russia, and the value of the name "Natalia" does not return true as alphaNumeric in the method below. Curious how someone approached such a problem! - Duncan

private boolean isAlphaNumeric(String str) { return str.matches("[\\w-']+"); } 
+7
source share
2 answers

You need to use the Unicode regex. for example \p{L}+ for any Unicode letter. For a more detailed look at the java doc for java.util.Pattern there is a section called unicode support . In addition, there are details: link

+10
source

In my case, I have to check if this name is written in Russian.

I ended up with this:

 private static final String ruNameRegEx = "[-][--]+"; 

and for the full name:

 private static final String ruNamePart = "[-][--]+"; private static final String ruFullNameRegEx = "\\s*[-][--]+\\s+(" + ruNamePart + "\\s+){1,5}" + ruNamePart + "\\s*";)"; 

The latter covers some complex cases:

 public class Test { Pattern ruFullNamePattern = Pattern.compile(ruFullNameRegEx); @Test public void test1() { assertTrue(isRuFullName("  ")); } @Test public void test2() { assertTrue(isRuFullName("    ")); } @Test public void test3() { assertTrue(isRuFullName("  ")); } @Test public void test4() { assertTrue(isRuFullName("  ")); } @Test public void test5() { assertFalse(isRuFullName("  ")); } @Test public void test6() { assertFalse(isRuFullName(" ..")); } @Test public void test7() { assertTrue(isRuFullName("- - ")); } @Test public void test8() { assertTrue(isRuFullName("  --")); } @Test public void test9() { assertTrue(isRuFullName("   ")); } private boolean isRuFullName(String testString) { Matcher m = ruFullNamePattern.matcher(testString); return m.matches(); } } 
+7
source

All Articles