简体   繁体   English

使用正则表达式在网页中查找多个值(组)

[英]Using Regex to find multiple values (groups) in webpage

I'm trying to retrieve 2 fields from a web page. 我正在尝试从网页中检索2个字段。 I'm using the following two patterns: 我正在使用以下两种模式:

string paternExperience = @"Experience\s\:\s\<strong\>(?<Level>.*?)\<";
string paternAccount = @"account_value\""\>(?<Account>.*?)\<";

and the following method to retrieve values and it works. 以及以下检索值的方法,它可以工作。

Regex.Matches(pageBody, patern..., RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Compiled |RegexOptions.Multiline);

I was trying to avoid using twice the method to retrieve 2 values, and I'm trying to create a pattern to get Level and Account in just one call of the Matches method. 我试图避免使用两次方法来检索2个值,并且试图创建一个模式来在一次Matches方法调用中获取Level和Account。 So I thought that something like the one below should work... 所以我认为下面的方法应该可行...

string paternBoth = @"Experience\s\:\s\<strong\>(?<Level>.*?)\< .* account_value\""\>(?<Account>.*?)\<";

But it doesn't work because I think that the two values are on diferent lines in html, so I added RegexOptions.SingleLine and now the method times out (the page has around 20kb). 但这是行不通的,因为我认为这两个值在html中的不同行上,所以我添加了RegexOptions.SingleLine,现在该方法超时了(该页面大约有20kb)。

Can you help me please with some advice? 你能帮我一些建议吗? Thank you! 谢谢!

You could try putting those 2 values in 1 variable, then just check that variable with your regex. 您可以尝试将这2个值放在1个变量中,然后只需使用正则表达式检查该变量即可。

I know it doesnt really make any sense but I try out things like that and sometimes it actually works. 我知道这真的没有任何意义,但是我尝试了类似的方法,有时它确实有效。 Never had this scenario but I did have any simular problems in the past. 从来没有这种情况,但过去我确实有任何类似的问题。 Might not be the best way. 可能不是最好的方法。 but sometimes making it work is more important then making it look pretty. 但是有时候让它正常工作比让它看起来更重要。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM