What is the {L} Unicode category?
Asked Answered
O

2

16

I came across some regular expressions that contain [^\\p{L}]. I understand that this is using some form of a Unicode category, but when I checked the documentation, I found only the following "L" categories:

Lu  Uppercase letter    UPPERCASE_LETTER
Ll  Lowercase letter    LOWERCASE_LETTER
Lt  Titlecase letter    TITLECASE_LETTER
Lm  Modifier letter     MODIFIER_LETTER
Lo  Other letter        OTHER_LETTER

What is L in this context?

Overscrupulous answered 11/5, 2011 at 19:20 Comment(0)
B
19

Taken from this link: http://www.regular-expressions.info/unicode.html

Check the Unicode Character Properties section.

\p{L} matches a single code point in the category "letter". If your input string is à encoded as U+0061 U+0300, it matches a without the accent. If the input is à encoded as U+00E0, it matches à with the accent. The reason is that both the code points U+0061 (a) and U+00E0 (à) are in the category "letter", while U+0300 is in the category "mark".

Brentonbrentt answered 11/5, 2011 at 19:28 Comment(2)
Thanks and +1 to you, too. Your comment on my comment/question to @Ned Batchelder's answer is appreciated.Overscrupulous
For an "official" reference to the "L" category, see here: unicode.org/reports/tr18/#General_Category_PropertySeismism
A
3

I don't see any explicit mention of it, but an example on this page indicates that \\p{L} means any letter:

Categories may be specified with the optional prefix Is: Both \p{L} and \p{IsL} denote the category of Unicode letters.

Arterial answered 11/5, 2011 at 19:28 Comment(4)
That's what I thought, too, but then why does the following regex replace (with a space) everything that's not a letter? String.replaceAll("[^\\p{L}]", " ")Overscrupulous
@uTubeFan: See you are using negation in ^\\p{L}. So when I do something like this "Test akd ^^%!~+_)".replaceAll("[^\\p{L}]", " ") then it will output Test akd . On the contrary if you do something like this "Test akd ^^%!~+_)".replaceAll("[\\p{L}]", " "); then the output will be ` ^^%!~+_)`Brentonbrentt
@Brentonbrentt Thanks! So, can I conclude from this that ^%!~+_ are not considered letters? (I am basically looking to replace all non-letters (except apostrophe ' as in wasn't) with a space, any suggestion?)Overscrupulous
@uTubeFan: Just saw your previous comment. Anyway you saved the work for me :)Brentonbrentt

© 2022 - 2024 — McMap. All rights reserved.