M
M
mk312014-08-14 07:57:17
Regular Expressions
mk31, 2014-08-14 07:57:17

C# and weird regexp work?

So, there is a task to parse the html of the page, namely to get two parameters, but for some reason only the first part is processed in the double regexp request, please help.

string pattern = "<td title.*?>(.*?)</td>|<textarea name=.*?>(.*?)</textarea>";

Only the first part is processed in the query: <td title.*?>(.*?)</td>, and spaces are returned instead of the second.
If swapped:
string pattern =  "<textarea name=.*?>(.*?)</textarea>|<td title.*?>(.*?)</td>";
then the first request will be processed <textarea name=.*?>(.*?)</textarea>and instead of the second it will return empty lines.
Only the first capture group is written to the result via groups[1].Value.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Panov, 2014-08-15
@mk31

Good afternoon
Use HTMLAgilityPack and do not use Regex for parsing html pages.

A
Artem Voronov, 2014-08-14
@newross

I'll just quote Stackoverflow

You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. so many times but it is not getting to me. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML. You will never make me crack. HTML is a language of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide. The cannot hold it is too late. The force of regex and HTML together in the same conceptual space will destroy your mind like so much watery putty. If you parse HTML with regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror.

S
Sumor, 2014-08-14
@Sumor

Grouping numbering does not take into account the "OR" sign. The first grouping is always the first, and the second grouping is always the second, regardless of what exactly worked in the regular expression.
Look at this code:

Regex rg = new Regex("a(a)|b(b)");            
Console.WriteLine(rg.Match("aa"));
Console.WriteLine(rg.Match("aa").Groups[1].Value);
Console.WriteLine(rg.Match("bb"));
Console.WriteLine(rg.Match("bb").Groups[1].Value);
Console.WriteLine(rg.Match("bb").Groups[2].Value);

His conclusion:
aa
a
bb

b

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question