我的程序有问题,因为当我尝试比较两个标签“和启用”>“时,它们会提取整个html标签。”我希望我的程序要做的是找到标签php源代码名为mystrings1,看看它们是否存在然后找到名为mystrings2的标签,其值为“enabled”,与每个匹配的mystrings1在同一行,然后提取mystrings1值。
在我的程序中,它会像这样读取整个标签:
[PHP]
<p id='mystrings1'>user data 1</p><p id="images">
<a href="images.php?id=1">Images</a> </td> | <a href="http://mylink.com">Link</a> </td> | <a href="delete.php?id=1">Delete</a> </td> | <span id="mystrings2">Enabled</td>
[/PHP]
这是表单代码:
#include "StdAfx.h"
#include "Form1.h"
using namespace MyProject;
System::Void Form1::timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
timer1->Enabled = false;
timer1->Stop();
try
{
String ^URL1 = "http://mysite.com/myscript.php?user=test&pass=test";
HttpWebRequest ^request1 = safe_cast<HttpWebRequest^>(WebRequest::Create(URL1));
HttpWebResponse ^response1 = safe_cast<HttpWebResponse^>(request1->GetResponse());
StreamReader ^reader1 = gcnew StreamReader(response1->GetResponseStream());
String ^str1 = reader1->ReadToEnd();
String ^pattern1 = "(<p id='mystrings1'>(.*?)</p>(.*?)<span id=\"mystrings2\">Enabled</td>)";
MatchCollection ^matches1 = Regex::Matches(str1, pattern1);
Match ^m1 = Regex::Match(str1, pattern1);
for each (Match ^x1 in matches1)
{
array<String^> ^StrArr1 = x1->Value->ToString()->Split();
MessageBox::Show(x1->Value->ToString());
}
}
catch (Exception ^ex)
{
}
}
有人可以告诉我如何将mystrings1和mystrings2之间的标签与值“enabled”进行比较以查看是否找到匹配项,然后显示带有mystrings1值的消息框?
非常感谢任何建议。
答案 0 :(得分:1)
此代码可能对您有所帮助
// set str1 to a constant string for testing
String^ str1 = "<p id='mystrings1'>user data 1</p><p id=\"images\"> <a href=\"images.php?id=1\">Images</a></td> | <a href=\"http://myhotlink.com\">Link</a> </td> | <a href=\"delete.php?id=1\">Delete</a> </td> | <span id=\"mystrings2\">Enabled</td>";
// the pattern is the key part to figure out
String ^pattern1 = "<p\\sid='mystrings1'>(?<mystrings1value>[^<>]+)</p>.*<span\\sid=\\\"mystrings2\\\">Enabled";
Regex^ r = gcnew Regex (pattern1, RegexOptions::IgnoreCase);
Match ^m1 = r->Match(str1);
if (m1->Success)
{
MessageBox::Show (m1->Groups["mystrings1value"]->Value);
}