0

I want such a validation that My String must be contains at least one Unicode letter. The character that will evaluate Character.isLetter() to true.

for example i want

~!@#$%^&*(()_+<>?:"{}|\][;'./-=` : false
~~1_~ : true
~~k_~ : true
~~汉_~ : true

I know i can use for-loop with Character.isLetter(), but i just don't want to do it.

And This is totally different from this since it only checks for the English alphabets, but in my case is about one unicode letter. It's not a same at all.

Mark Melgo
  • 1,477
  • 1
  • 13
  • 30
Jude KIM
  • 109
  • 1
  • 4
  • How does this ```~~汉_~ : true``` evaluate to true? Your 'letter' is multi language? Not just English alphabets? – Mark Melgo Mar 05 '19 at 02:21
  • Not alphabet but Unicode letter. Try Character.isLetter('汉'). It will return true. And that is what i want. – Jude KIM Mar 05 '19 at 02:35
  • related https://stackoverflow.com/questions/1193200/how-can-i-check-whether-a-byte-array-contains-a-unicode-string-in-java and https://stackoverflow.com/questions/36889924/regex-for-matching-unicode-pattern –  Mar 05 '19 at 04:13

1 Answers1

1

You can try to use this regex "\\p{L}|[0-9]"

To better understand Unicode in Regex read this.

Usage code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String args[]) {
        // String to be scanned to find the pattern.
        String line = "~!@#$%^&*(()_+<>?:\"{}|\\][;'./-=`";
        String pattern = "\\p{L}|[0-9]"; // regex needed

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);
        System.out.print("String \"" + line + "\" results to ");
        if (m.find()) {
            System.out.println("TRUE -> Found value: " + m.group(0));
        } else {
            System.out.println("FALSE");
        }
        line = "~~1_~";
        m = r.matcher(line);
        System.out.print("String \"" + line + "\" results to ");
        if (m.find()) {
            System.out.println("TRUE -> Found value: " + m.group(0));
        } else {
            System.out.println("FALSE");
        }
        line = "~~k_~";
        m = r.matcher(line);
        System.out.print("String \"" + line + "\" results to ");
        if (m.find()) {
            System.out.println("TRUE -> Found value: " + m.group(0));
        } else {
            System.out.println("FALSE");
        }
        line = "~~汉_~";
        m = r.matcher(line);
        System.out.print("String \"" + line + "\" results to ");
        if (m.find()) {
            System.out.println("TRUE -> Found value: " + m.group(0));
        } else {
            System.out.println("FALSE");
        }
    }

}

Result:

String "~!@#$%^&*(()_+<>?:"{}|\][;'./-=`" results to FALSE
String "~~1_~" results to TRUE  
String "~~k_~" results to TRUE -> Found value: k
String "~~汉_~" results to TRUE -> Found value: 汉
Mark Melgo
  • 1,477
  • 1
  • 13
  • 30