简体   繁体   中英

How to extract this part of the string with regex

I have the following string

  var tabContents = {"1":"<div class=\"ProductDetail\"><h2 class=\"h2_style\" style=\"margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>","2":"","3":"","4":""};

Now i would like to get this part

<div class=\"ProductDetail\"><h2 class=\"h2_style\" style=\"margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>

So that part starts with "1":" and ends with ","2"

How can i get the string between these two marking points ?

C# .net 4.5

Use capturing groups or lookarounds.

"1":"(.*?)","2"

Use the above regex and get the string you want from group index 1.

DEMO

OR

(?<="1":").*?(?=","2")

Use the above regex and get the string you want from group index 0.

  • (?<="1":") Positive lookbehind which asserts that the match must be preceded by "1":" .

  • .*? Non-greedy match of zero or more occurrences of any character.

  • (?=","2") Positive lookahead which asserts that the match must be followed by ","2"

DEMO

String input = @"var tabContents = {""1"":""<div class=\""ProductDetail\""><h2 class=\""h2_style\"" style=\""margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>"",""2"":"""",""3"":"""",""4"":""""};";
Regex rgx = new Regex(@"""1"":""(.*?)"",""2""");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[1].Value);

Output:

<div class=\"ProductDetail\"><h2 class=\"h2_style\" style=\"margin: 0px 0px 15px; padding: 0px; border: 0px; vertical-align: baseline; font-weight: normal; color: rgb(0, 102, 153); letter-spacing: 0.3px; font-size: 16px; text-align: center; font-family: 'Lucida Grande', sans-serif; font-style: normal; font-variant: normal; line-height: 16px; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webv><\/div>

IDEONE

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