更新时间:2023-11-11 13:58:16
使用以下Regex
:
Use the followingRegex
:
public static Regex regex = new Regex(
@"\<td.*\>(?<num>.*)\<\/td\>",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
这将为您提供名为num
的命名匹配.
Which will give you named matches called num
.
Mehdi Gholam 给出的解决方案1非常好.唯一的问题是regex
中给出的.*
会捕获包括< and >
在内的所有内容,因此我们只能得到最后一个数字,即130.0.因此,必须将.*
替换为[^<>]*
.
此外,在用matches
捕获所有数字之后,在每个匹配项中捕获的数字将为Groups[1]
.因此,OP在上述注释中声明的Groups[2], Groups[3]
可能无法给出正确的结果.
因此,以下代码可用于根据需要设置值.
The Solution 1 given by Mehdi Gholam is excellent. Only thing is that.*
given in theregex
captures everything including< and >
so that we get only the last number i.e. 130.0. So,.*
has to be replaced by[^<>]*
.
Further, after capturing all the numbers withmatches
, the captured number will beGroups[1]
in each match. So,Groups[2], Groups[3]
stated by OP in the comment above, may not give the correct result.
Hence, the following code can be used to set the values as required.
Regex regex = new Regex(@"\<td[^<>]*\>([^<>]*)\<\/td\>",RegexOptions.IgnoreCase | RegexOptions.Multiline);
var matches = regex.Matches(comp);
if (matches.Count==3){
string v=matches[0].Groups[1].Value;
string r=matches[1].Groups[1].Value;
string s=matches[2].Groups[1].Value;
}