简体   繁体   English

带有\\ w和德语特殊字符的C#正则表达式?

[英]C# regular expression with \w and german special characters?

I need to capture some text with \\w, but this text can contain german Umlaute (Ü Ä Ö), which causes my regex to fail. 我需要使用\\ w捕获一些文本,但是该文本可能包含德语Umlaute(ÜÄÖ),这会导致我的正则表达式失败。 How can I extend the regular expression to match these cases? 如何扩展正则表达式以匹配这些情况?

The regex: 正则表达式:

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

I know I can simply add Ü Ö Ä to [\\w\\s]*, but that is not really extensible. 我知道我可以简单地将ÜÖÄ添加到[\\ w \\ s] *,但这并不是真正可扩展的。

In .NET, \\w will match accented letters by default ( reference ). 在.NET中, \\w将默认匹配带重音的字母( 参考 )。 It looks like you made a mistake elsewhere, but as your code in the question is not syntactically valid, I don't exactly know where. 看起来您在其他地方犯了一个错误,但是由于问题中的代码在语法上无效,因此我不知道该在哪里。

Here's the same code that I just cleaned up, it should work: 这是我刚刚清理过的相同代码,它应该可以工作:

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\"");

The unescaped regex is printfile ps\\s*=\\s*"[\\w\\s]*\\.ps\\s*" . 未转义的正则表达式是printfile ps\\s*=\\s*"[\\w\\s]*\\.ps\\s*"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM