我正在经历这个问题 C#, Regex.Match whole words
它表示匹配整个单词使用“\ bpattern \ b” 这适用于匹配整个单词而没有任何特殊字符,因为它仅用于单词字符!
我需要一个表达式来匹配带有特殊字符的单词。我的代码如下
class Program
{
static void Main(string[] args)
{
string str = Regex.Escape("Hi temp% dkfsfdf hi");
string pattern = Regex.Escape("temp%");
var matches = Regex.Matches(str, "\\b" + pattern + "\\b" , RegexOptions.IgnoreCase);
int count = matches.Count;
}
}
但由于%而失败。我们有解决方法吗? 可以有其他特殊字符,如'space','(',')'等
答案 0 :(得分:5)
如果您有非单词字符,则无法使用\b
。您可以使用以下
@"(?<=^|\s)" + pattern + @"(?=\s|$)"
编辑:正如Tim在评论中提到的那样,你的正则表达式失败正是因为\b
无法匹配%
与其旁边的空格之间的边界,因为两者都是他们是非单词字符。 \b
仅匹配单词字符和非单词字符之间的边界。
详细了解字词边界here。
<强>解释强>
@"
(?<= # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
# Match either the regular expression below (attempting the next alternative only if this one fails)
^ # Assert position at the beginning of the string
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
\s # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
)
temp% # Match the characters “temp%” literally
(?= # Assert that the regex below can be matched, starting at this position (positive lookahead)
# Match either the regular expression below (attempting the next alternative only if this one fails)
\s # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
| # Or match regular expression number 2 below (the entire group fails if this one fails to match)
$ # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
"
答案 1 :(得分:2)
如果模式可以包含Regex特有的字符,请先通过Regex.Escape运行。
你这样做了,但不转义你搜索的字符串 - 你不需要它。
答案 2 :(得分:1)
output = Regex.Replace(output, "(?<!\w)-\w+", "")
output = Regex.Replace(output, " -"".*?""", "")
答案 3 :(得分:0)