[英]Regex is not matching for string c#
string val = "VFC - [C:\study\Run20315.5000]"
string pattern = "VFC - *C:\\study\\Rund.*"
I have written below expression but its getting false. 我已经写了下面的表达式,但是它变得错误了。
bool Match= Regex.IsMatch(val, pattern)
You forgot about the square brackets, the backslash before d
is missing if you planned to match a digit and the backslashes must be doubled - or better - use a verbatim string literal. 您忘记了方括号,如果您计划匹配一个数字,则
d
之前的反斜杠会丢失,并且反斜杠必须加倍-或更佳-使用逐字字符串。 Also, note that *
is a quantifier that makes the pattern preceding it match 0 or more times. 另外,请注意,
*
是一个量词 ,可使前面的模式匹配0次或多次。 If you need to match arbitrary text between two patterns, use .*
or .*?
如果需要在两个模式之间匹配任意文本,请使用
.*
或.*?
and in case there can be line breaks, compile the pattern with RegexOptions.Singleline
: 如果可能会有换行符,请使用
RegexOptions.Singleline
编译模式:
string pattern = @"VFC - .*C:\\study\\Run\d";
bool Match= Regex.IsMatch(val, pattern, RegexOptions.Singleline);
See the .NET regex demo and the Regulex graph : 请参见.NET regex演示和Regulex图 :
Details 细节
VFC -
- literal VFC -
substring VFC -
文字VFC -
子字符串 .*
- any zero or more chars as many as possible .*
-尽可能多的零个或多个字符 C:\\\\study\\\\Run
- a C:\\study\\Run
substring C:\\\\study\\\\Run
- C:\\study\\Run
子字符串 \\d
- a digit. \\d
一个数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.