简体   繁体   中英

How to use Regex to identify specific line? c#

I'm new to regex and still don't understand it well

I need to identify a line but for some reason I always gets false when using regex

line to identify structure: Screen Message: FULL PATH \ FILENAME.PNG

example:

Screen Message: c:\program files (x86)\regression machine work\reports\156487/04-37-71_47-38-141\Screenmessages\63445646476476767574.FAR.png

tried to go for the prefix ("Screen Message: ") and the suffix (".FAR.png") but couldn't get it to work

Regex.Match(Text, $"(^Screen Message: )+[0-9]+[a-z]\+{.FAR.png}$", RegexOptions.IgnoreCase).Success;

Any help would be appreciated thanks

You can use a regex like

Screen Message:\s*(?<folder>.*)\\(?<filename>.*\.FAR\.png)

See the regex demo . Details :

  • Screen Message: - a string
  • \s* - zero or more whitespace chars
  • (?<folder>.*) - Group "folder": any zero or more chars as many as possible
  • \\ - a \ char
  • (?<filename>.*\.FAR\.png) - Group "filename": any zero or more chars as many as possible, and then a .FAR.png substring

See a C# demo :

var pattern = @"Screen Message:\s*(?<folder>.*)\\(?<filename>.*\.FAR\.png)";
var input = @"Screen Message: c:\program files (x86)\regression machine work\reports\156487/04-37-71_47-38-141\Screenmessages\63445646476476767574.FAR.png";
var match = Regex.Match(input, pattern);
if (match.Success)
{
    Console.WriteLine("Folder: '{0}', filename: '{1}'", 
        match.Groups["folder"].Value, match.Groups["filename"].Value);
}

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