6

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?

5
  • I copied your word and put into string, I used regular expression \w+ and I can get word correctly. I am testing with C#. Feb 9, 2012 at 2:31
  • Im using Java. Also i have tested with myregextester.com and still it doesnt recognize whole word.
    – jyriand
    Feb 9, 2012 at 2:37
  • Maybe you want to look at Unicode letters? \p{L}+
    – Wiseguy
    Feb 9, 2012 at 2:40
  • Yes, I go that site and test. Cannot get correct result. I tested my C# code again. I got word as expected. string test = "Aiavärav abc 123"; Regex reg = new Regex(@"\w+"); Console.Write(reg.Match(test)); I got "Aiavärav" Feb 9, 2012 at 2:43
  • corrected "non-latin" since "ä" is still technically a latin-derived character.
    – Sled
    Feb 9, 2012 at 2:57

1 Answer 1

13

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".)

5
  • Be aware that embedding (?U) "... may impose a performance penalty." from docs.oracle.com/javase/7/docs/api/java/util/regex/…
    – reallynice
    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.
    – ruakh
    Sep 26, 2014 at 3:06
  • Maybe I'm wrong, but in the UNICODE_CHARACTER_CLASS link i read also The 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)
    – reallynice
    Sep 26, 2014 at 7:01
  • @niconic: The documentation states that this mode can be enabled via (?U), but the statement that the UNICODE_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.
    – ruakh
    Sep 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).
    – reallynice
    Oct 3, 2014 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.