[英]C# regular expression with \w and german special characters?
我需要使用\\ w捕獲一些文本,但是該文本可能包含德語Umlaute(ÜÄÖ),這會導致我的正則表達式失敗。 如何擴展正則表達式以匹配這些情況?
正則表達式:
Regex PFileRegex = new Regex("printfile ps *\\t*= *\\t*\"[\\w\\s]*.ps\\s*\"", RegexOptions.IgnoreCase);
Match PFilematch = PFileRegex.Match("printfile ps = EXAMPLE Ä.ps"); //false
Match PFilematch = PFileRegex.Match("printfile ps = EXAMPLE.ps"); //true
我知道我可以簡單地將ÜÖÄ添加到[\\ w \\ s] *,但這並不是真正可擴展的。
在.NET中, \\w
將默認匹配帶重音的字母( 參考 )。 看起來您在其他地方犯了一個錯誤,但是由於問題中的代碼在語法上無效,因此我不知道該在哪里。
這是我剛剛清理過的相同代碼,它應該可以工作:
var fileRegex = new Regex(@"printfile ps\s*=\s*""[\w\s]*\.ps\s*""", RegexOptions.IgnoreCase);
var fileMatch1 = fileRegex.Match("printfile ps = \"EXAMPLE Ä.ps\"");
var fileMatch2 = fileRegex.Match("printfile ps = \"EXAMPLE.ps\"");
未轉義的正則表達式是printfile ps\\s*=\\s*"[\\w\\s]*\\.ps\\s*"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.