madmike_de
02/18/2025, 10:03 AM<cfscript>
function containsCyrillic(text) {
return REFind("[\u0400-\u04FF]", text) > 0;
}
text = "Пример";
if (containsCyrillic(text)) {
WriteOutput("Der Text enthält kyrillische Zeichen.<br>");
} else {
WriteOutput("Der Text enthält keine kyrillischen Zeichen.<br>");
}
</cfscript>
Any idea what is wrong here? I can use ``javaCast("string",`
arguments.searchTerm).matches(".*[\p{InCyrillic}].*") which works, but wonder if this is just a bug or is the problem in front of the monitor.Evil Ware
02/18/2025, 1:24 PM<!--- Convert the string to a byte array in UTF-8 encoding --->
<cfset byteArray = createObject("java", "java.nio.charset.StandardCharsets").UTF_8.encode(text).array()>
<!--- Use the juniversalchardet library to detect encoding --->
<cfset charsetDetector = createObject("java", "org.mozilla.universalchardet.UniversalDetector").new()>
<cfset charsetDetector.handleData(byteArray, 0, arrayLen(byteArray))>
<cfset charsetDetector.dataEnd()>
<!--- check encoding --->
<cfset detectedEncoding = charsetDetector.getDetectedCharset()>bdw429s
02/18/2025, 7:21 PMjeffcoughlin
02/18/2025, 7:22 PMjeffcoughlin
02/18/2025, 7:22 PMbdw429s
02/18/2025, 7:23 PMjeffcoughlin
02/18/2025, 7:23 PM\p{}) in REFind() and does not properly interpret \u0400-\u04FF. Using Java’s matches() method is the most reliable workaround..
Lucee uses Apache ORO for regex, which does not fully support Unicode character classes in the same way as Java’s Pattern class.
Since you already have a working solution using Java’s matches() with \p{InCyrillic}, another way to fix this in Lucee (without switching to Java regex) is to use REFindNoCase() with charset="UTF-8" and specify the range explicitly.
[2:18 PM] jeffcoughlin
Option 1: Using REFInd():
function containsCyrillic(text) {
return REFind("[\x{0400}-\x{04FF}]", text, "ALL") > 0;
}
text = "Пример";
if (containsCyrillic(text)) {
WriteOutput("Der Text enthält kyrillische Zeichen.<br>");
} else {
WriteOutput("Der Text enthält keine kyrillischen Zeichen.<br>");
}
[2:19 PM] jeffcoughlin
Option 2: Using javaCast("string", text).matches() (Works in Lucee)
Since Lucee runs on Java, you can leverage Java’s Unicode support:
function containsCyrillic(text) {
return javaCast("string", text).matches(".*\\p{InCyrillic}.*");
}
text = "Пример";
if (containsCyrillic(text)) {
WriteOutput("Der Text enthält kyrillische Zeichen.<br>");
} else {
WriteOutput("Der Text enthält keine kyrillischen Zeichen.<br>");
}
[2:20 PM] jeffcoughlin
My advice is to use option 2. It's the most reliable solution because Java's regex engine natively supports Unicode character properties.
[2:21 PM] jeffcoughlin
Удачи!bdw429s
02/18/2025, 7:23 PMjeffcoughlin
02/18/2025, 7:23 PMbdw429s
02/18/2025, 7:24 PMbdw429s
02/18/2025, 7:24 PMjeffcoughlin
02/18/2025, 7:25 PMjeffcoughlin
02/18/2025, 7:26 PMbdw429s
02/18/2025, 7:27 PMjeffcoughlin
02/18/2025, 7:31 PMagentk
02/18/2025, 7:31 PMrodyon
02/19/2025, 11:16 AMrodyon
02/19/2025, 11:16 AMrodyon
02/19/2025, 11:20 AM