Question 1:
问题1:
For example,
例如,
... "ab'cd" ... 'ab"cd' ...
It should get ab'cd
and ab"cd
respectively. There may be many quoted strings.
它应该分别得到ab'cd和ab“cd。可能有很多带引号的字符串。
Question 2:
问题2:
And how to extract text <td>...</td> text
from the following string?
以及如何从以下字符串中提取文本 ... 文本?
<abc>text <td>...</td> text</abc>
<xyz>text <td>...</td> text</xyz>
<def>text <td>...</td> text</def>
abc, def, xyz may be nested in <td>
.
abc,def,xyz可以嵌套在中。
4 个解决方案
#1
2
regex:
正则表达式:
(?:"(?<text>[^"]*)")|(?:'(?<text>[^']*)')
(: “( <文本> [^?”] *)“?)|(?: '?( <文本> [^'] *)')
and the whole snippet:
和整个片段:
Regex regex = new Regex(
@"(?:""(?<text>[^""]*)"")|(?:'(?<text>[^']*)')",
RegexOptions.None
);
Testable on my blog (requires silverlight)
可以在我的博客上测试(需要silverlight)
#2
1
Looks like this is a 2-in-1 question right?
看起来这是一个二合一的问题吧?
My answer to 1 is
我对1的回答是
((.*)['"](.*))
then for #2
那么#2
<(abc|xyz|def)>(.*)<\/(abc|xyz|def)>
abc | xyz | def <- imagine it like this
abc | xyz | def < - 想象一下这样
dont forget to TRIM the spaces before you use the results
不要忘记在使用结果之前修剪空格
#3
0
for first question use:
对于第一个问题使用:
("(?<content>[^"]*)")|('(?<content>[^']*)')
( “?( <内容> [^”] *)“)|( '?( <内容> [^'] *)')
for second question:
第二个问题:
>(?<content>\w*\s*<td>.*</td>[\w\s]*)<
>(? <内容> \ W * \ S * * [\ W \ S *)<
and get group named content for both.
并为两者获取组命名内容。
#4
0
I figured it out - using back reference.
我想通了 - 使用后退参考。
(["'])(?<q>.+?)\1
#1
2
regex:
正则表达式:
(?:"(?<text>[^"]*)")|(?:'(?<text>[^']*)')
(: “( <文本> [^?”] *)“?)|(?: '?( <文本> [^'] *)')
and the whole snippet:
和整个片段:
Regex regex = new Regex(
@"(?:""(?<text>[^""]*)"")|(?:'(?<text>[^']*)')",
RegexOptions.None
);
Testable on my blog (requires silverlight)
可以在我的博客上测试(需要silverlight)
#2
1
Looks like this is a 2-in-1 question right?
看起来这是一个二合一的问题吧?
My answer to 1 is
我对1的回答是
((.*)['"](.*))
then for #2
那么#2
<(abc|xyz|def)>(.*)<\/(abc|xyz|def)>
abc | xyz | def <- imagine it like this
abc | xyz | def < - 想象一下这样
dont forget to TRIM the spaces before you use the results
不要忘记在使用结果之前修剪空格
#3
0
for first question use:
对于第一个问题使用:
("(?<content>[^"]*)")|('(?<content>[^']*)')
( “?( <内容> [^”] *)“)|( '?( <内容> [^'] *)')
for second question:
第二个问题:
>(?<content>\w*\s*<td>.*</td>[\w\s]*)<
>(? <内容> \ W * \ S * * [\ W \ S *)<
and get group named content for both.
并为两者获取组命名内容。
#4
0
I figured it out - using back reference.
我想通了 - 使用后退参考。
(["'])(?<q>.+?)\1