Lets say I have a word: "Aiavärav". The expression \w+
should capture this word, but the letter "ä" cuts the word in half. Instead of "Aiavärav", I get "Aia". What is the correct regex for words that contain those non-ascii letters?
1 Answer
According to the documentation, \w
only matches [a-zA-Z_0-9]
unless you specify the UNICODE_CHARACTER_CLASS
flag:
Pattern.compile("\\w+", Pattern.UNICODE_CHARACTER_CLASS)
or embed a (?U)
in the pattern:
Pattern.compile("(?U)\\w+")
either of which requires JDK 1.7 (i.e., Java 7).
If you don't have Java 7, you can generalize \w
to Unicode by using \p{L}
("letter"; like [a-zA-Z]
, but not ASCII-specific) and \p{N}
("number"; like [0-9]
, but not ASCII-specific):
Pattern.compile("[\\p{L}_\\p{N}]+")
But it sounds like maybe you're looking for actual words, in the normal sense (as opposed to the programming-language sense), and don't need to support digits and underscores? In that case, you can just use \p{L}
:
Pattern.compile("\\p{L}+")
(By the way, the curly brackets are actually optional — you can write \pL
instead of p{L}
and \pN
instead of \p{N}
— but people usually include them anyway, because they're required for multi-letter categories like \p{Lu}
"uppercase letter".)
-
Be aware that embedding (?U) "... may impose a performance penalty." from docs.oracle.com/javase/7/docs/api/java/util/regex/… Sep 25, 2014 at 8:37
-
@niconic: The documentation that you link to is actually referring to
UNICODE_CHARACTER_CLASS
rather than(?U)
, but either way, I think the expensive part is matching using Unicode character properties (compared to the groups of ASCII characters). Since that is exactly what the OP is trying to do, it's not really a "performance penalty" in his/her case, and would presumably apply to any approach (s)he might take.– ruakhSep 26, 2014 at 3:06 -
Maybe I'm wrong, but in the
UNICODE_CHARACTER_CLASS
link i read alsoThe UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded flag expression (?U).
, so I think that we're talking about the same stuff. Am I misunderstanding something? Of course your answer fits the need, I just wanted to point out that (because I found myself in a vaguely similar case and performance for me was essential) Sep 26, 2014 at 7:01 -
@niconic: The documentation states that this mode can be enabled via
(?U)
, but the statement that theUNICODE_CHARACTER_CLASS
flag may impose a performance penalty is in a separate paragraph, so does not apply to(?U)
. To be sure, I believe the same statement could be made of(?U)
; but my concern about your comment is that my answer names three approaches, and your comment says that one of them may impose a performance penalty, as though you were advocating the other two approaches instead. That's highly misleading, since (I believe) every approach has the same penalty.– ruakhSep 27, 2014 at 3:24 -
Ok I got, my comment could be misleading: when I wrote it I didn't want to target precisely the ?U flag but what it means (i.e. all approaches). Oct 3, 2014 at 13:37
\p{L}+