views:

73

answers:

2

I m trying to match unicode characters in Java.

Input String: informa

String to match : informátion

So far I ve tried this:

Pattern p= Pattern.compile("informa[\u0000-\uffff].*", (Pattern.UNICODE_CASE|Pattern.CANON_EQ|Pattern.CASE_INSENSITIVE));
    String s = "informátion";
    Matcher m = p.matcher(s);
    if(m.matches()){
        System.out.println("Match!");
    }else{
        System.out.println("No match");
    }

It comes out as "No match". Any ideas?

+5  A: 

Is it because informa isn't a substring of informátion at all?

How would your code work if you removed the last a from informa in your regex?

Austin Fitzpatrick
informa\u0301 works in the pattern string. This has to do with the Pattern.CANON_EQ case.
ankimal
Forgot to put in the link for this, http://java.sun.com/docs/books/tutorial/essential/regex/pattern.html (Pattern.CANON_EQ)
ankimal
+2  A: 

The term "Unicode characters" is not specific enough. It would match every character which is in the Unicode range, thus also "normal" characters. This term is however very often used when one actually means "characters which are not in the printable ASCII range".

In regex terms that would be [^\x20-\x7E].

boolean containsNonPrintableASCIIChars = string.matches(".*[^\\x20-\\x7E].*");

Depending on what you'd like to do with this information, here are some useful follow-up answers:

BalusC
The java.text.Normalizer seems like the way to go(bullet 2). Unicode matching just doesnt seem to work as expected and I might be hit with a performance penalty even if it did.
ankimal
If your *actual* functional requirement is "get rid of diacritical marks", then it's indeed the way to go. Your initial question was only not formulated like that :)
BalusC
I think the question wasnt crystal clear. The goal was to be able to match "information" with "informátion", thus the ability to match 'a' with any forms of a like 'á','å' etc. Removing diacritical marks and then matching seems to be the way to go.
ankimal