maya li
maya li

Reputation: 33

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

Upvotes: 1

Views: 49

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

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);
}

Upvotes: 1

Related Questions