简体   繁体   中英

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. Also, note that * is a quantifier that makes the pattern preceding it match 0 or more times. 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 :

string pattern = @"VFC - .*C:\\study\\Run\d";
bool Match= Regex.IsMatch(val, pattern, RegexOptions.Singleline);

See the .NET regex demo and the Regulex graph :

在此处输入图片说明

Details

  • VFC - - literal VFC - substring
  • .* - any zero or more chars as many as possible
  • C:\\\\study\\\\Run - a C:\\study\\Run substring
  • \\d - a digit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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