問題描述
我正在尋找一個 ColdFusion 或 Java 正則表達式(用于替換函數),它只匹配數字 [0-9]、字母 [az],但不包含 ASCII 葡萄牙語 字母(unicode latin,如 ?
和 ?
).
I'm looking for a ColdFusion or Java regex (to use in a replace function) that will only match numbers [0-9], letters [a-z], but include none ASCII Portuguese letters (unicode latin, like ?
and ?
).
有些是這樣的:
str = reReplaceNoCase(str, "match none number/letter but keep unicode latin chars", "", "ALL");
輸入字符串:informa??o 123 ?:#$%"
期望的結果:informa??o 123"
我知道我可以用 [az][0-9]
匹配字母和數字,但這不匹配 ?
和 ? 等字母
.
I know I can match letters and numbers with [a-z][0-9]
, but this doesn't match letters such as ?
and ?
.
推薦答案
試試字母數字字符類:w
,它應該匹配字母、數字和下劃線.
Try alphanumeric character class: w
, it should match letters, digits, and underscores.
您也可以使用特殊的命名類 p{L}
(我不知道,Java RegEx 解析器是否支持它).因此,在 C# 中,您的任務可以使用以下代碼完成:
Also you can use special named class p{L}
(I don't know, does Java RegEx parser support it).
So in C# your task can be done using following code:
var input = "informa??o 123 ?:#$%";
var result = Regex.Replace(input, @"[^p{L}s0-9]", string.Empty);
Regex [^p{L}s0-9]
表示:該類中的任何字符not(所有字母、空格、數字).因此它在您的示例 ?:#$%
中匹配,我們可以用空字符串替換這些字符.
Regex [^p{L}s0-9]
means: any character not in this class (all letters, white space, digits). Thereby it matches in your example ?:#$%
and we can replace these characters with empty string.
這篇關于如何在 ColdFusion 或 Java 正則表達式中匹配拉丁 unicode 字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!