Hey Im trying to extract certain information from a string. The String looks like
Name: music mix.mp3 Size: 2356KB
I would like to extract the file name only with the extension.
I dont have much knowledge in regex, so I was hoping to get some help here. Thanks!
Please check this example:
const string str = "Name: music mix.mp3 Size: 2356KB";
var match = Regex.Match(str, "Name: (.*) Size:");
Console.WriteLine("Match: " + match.Groups[1].Value);
Solution using regex lookaround feature.
String sourcestring = "Name: music mix.mp3 Size: 2356KB";
Regex re = new Regex(@"(?<=^Name: ).+(?= Size:)");
Match m = re.Match(sourcestring);
Console.WriteLine("Match: " + m.Groups[0].Value);
This is regex
Name:\s*(?<FileName>[\w\s]+.\w{3})
this regex return the music mix.mp3 in group if the name of file is with white space
string strRegex = @"Name:\s*(?<FileName>[\w\s]+.\w{3})";
Regex myRegex = new Regex(strRegex);
string strTargetString = @"Name: music mix.mp3 Size: 2356KB";
Match myMatch = myRegex.Match(strTargetString);
string fileName = myMatch.Groups["FileName"].Value;
Console.WriteLine(fileName);
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.