简体   繁体   中英

regex extract value from the string between delimiters

I have a large String and I need to extract String value from it. String value is located between delimiters

category = '

and

';

This is my regex, but I need to avoid outputing delimiters.

String productCategory = Regex.Match(html, @"category = '(.*?)';").Value;

This is the exampe category = 'Video Cards';

and I need to extract Video Cards

What you can use is the lookahead and lookbehind operators, so you end up with something like:

string pattern = @"(?<=category = ').*(?=';)";
string productCategory = Regex.Match(html, pattern ).Value;

It's also worth mentioning that parsing HTML with regexes is a bad idea . You should use an HTML parser to parse HTML.

Have you considered using the MatchObj.Groups property? If you test your current regex at a testing site like Derek Slager's , you'll notice exactly what you want is the first Group. You should simply be able to invoke the first Group and get what you need.

productCategory.Groups[0].Value

您要提取组:

String productCategory = Regex.Match(html, @"category = '(.*?)';").Groups[1].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