- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有这样的 php 函数,我想翻译成 C++:
protected function htmlTag($content, $tag, $attrName, $attrValue, $valueName)
{
preg_match_all("#<{$tag}[^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*$valueName=['\"](.+?)['\"][^>]*/?>#i", $content, $matches1);
preg_match_all("#<{$tag}[^>]*$valueName=['\"](.+?)['\"][^>]*$attrName=['\"].*?$attrValue.*?['\"][^>]*/?>#i", $content, $matches2);
$result = array_merge($matches1[1], $matches2[1]);
return empty($result)?false:$result[0];
}
使用示例:
$location = $this->htmlTag($content, 'meta', 'http-equiv', 'X-XRDS-Location', 'content');
$server = $this->htmlTag($content, 'link', 'rel', 'openid.server', 'href');
$delegate = $this->htmlTag($content, 'link', 'rel', 'openid.delegate', 'href');
(内容是 $content= curl_exec($curl);
的结果)
preg_match_all
- 在主题中搜索与模式中给定的正则表达式的所有匹配项,并按照标志指定的顺序将它们放入匹配项中。找到第一个匹配后,后续搜索从最后一个匹配结束继续。
如何使用 boost::regexp 翻译它?
最佳答案
像这样:
boost::optional<std::string> htmlTag(const std::string& content,
const std::string& tag, const std::string& attrName,
const std::string& attrValue, const std::string& valueName)
{
const std::string
expr1 = boost::format("#<%1[^>]*%2=['\"].*?%3.*?['\"][^>]"
"*%4=['\"](.+?)['\"][^>]*/?>#i")
% tag % attrName % attrValue % valueName,
expr2 = boost::format("#<%1[^>]*%2=['\"](.+?)['\"][^>]*"
"%3=['\"].*?%4.*?['\"][^>]*/?>#i")
% tag % attrName % attrValue % valueName;
boost::match_results<std::string::const_iterator>
matches1, matches2, result;
// do the searches (note: these probably need to be loops as shown at the bottom of this page:
// http://www.boost.org/doc/libs/1_47_0/libs/regex/doc/html/boost_regex/ref/regex_search.html
if (!regex_search(content, matches1, expr1))
return boost::none;
if (!regex_search(content, matches2, expr2))
return boost::none;
result = // merge matches1[1] and matches2[1] somehow
if (result.empty())
return boost::none;
else
return result[0];
}
我确定我弄错了一些细节(一方面,我认为你需要根据评论一遍又一遍地调用 regex_search),但希望你能解决这些细节并发布你完成的解决方案。
关于php - PHP`preg_match_all` 函数的 Boost::regexp 模拟是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6725035/
我有这个正则表达式 {{([ a-zA-Z0-9_\-]+)\s*(?:\[([ a-zA-Z0-9_\-]+)\]\s*)*}} 我需要匹配如下字符串: {{word with spaces}} {
我有这个子模式: 它的结果是这样的输出: php test.php Array ( [0] => Array ( [0] => d
你好我想提取链接 我想要一个正则表达式给我/portal/clients/show/entityId/2121最后2121的数字在其他链接中不同有什么想法吗? 最佳答案 Simple PHP HTML
我试图提取所有 img 来自 HTML 字符串的标签。看代码 $d1 = file_get_contents("http://itcapsule.blogspot.com/feeds/post
我正在尝试为我们的防火墙创建报告功能。 防火墙规则以 json 格式存储。 这是字符串的示例。 [{"id":1,"enabled":true,"description":"TEMP","matche
我需要从文本 block 中提取一组预定义的主题标签,然后提取紧随其后的数字(如果有)。例如。我需要从“带有#other30 标签的测试字符串”中提取 30。我认为 preg_match_all 是正
我正在尝试从可能很大的文本中提取 ID,我错过了什么? preg_match_all('/(ID\s\d+)/', "ID 20380843, ID 20675712", $matches); pr
我正在尝试抓取 html 并抓取 之间的项目标签。由于某种原因( ),某些标签以大写形式出现,并且被我的模式忽略。我如何告诉我的模式忽略大小写。 我当前的模式是: preg_match_all("
我有一个从另一个网站读取数据的脚本,但我希望该脚本从下向上而不是从上向下获取数据。 例如,如果结果是: 结果#1结果#2结果#3 我想先得到#3,然后是2,然后是1。目前它得到的是1,然后是2,然后是
我有一个网址: https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2o
在使用正则表达式时遇到问题(它们不是我的强项)。我试图匹配 {{ 和 }} 之间的所有字符串,但如果一组括号出现在同一行上,则会将其视为单个匹配...示例: $string = " Hello,
我有一个始终遵循以下格式的字符串: This Fee Name : * Fee Id * Fee Amount $* is required for this activity 例子: Thi
我在使用 php (preg_match_all) 中的正则表达式时遇到了一些问题。 我正在使用以下代码查找由 <>: 封装的电子邮件,即: preg_match_all(":", $body,$ma
我有这个脚本可以从另一个网站读取数据,但我希望脚本从下到上而不是从上到下获取数据。 例如如果结果是: 结果 #1结果 #2结果 #3 我想先得到#3,然后是 2,然后是 1。目前它是 1,然后是 2,
我有一个网址: https://my.site.com/u/0/ac?export=download&confirm=45vy&id=qNhdhk1jejhXLexLpY3RiDY2o
我正在尝试获取点后的值,并且我想获取所有这些点(每个都是它们自己的键/值)。 以下是我正在运行的内容: $string = "div.cat.dog#mouse"; preg_match_all("/
这个问题已经有答案了: "Regular Expression is too large" error in PHP (3 个回答) 已关闭 3 年前。 我该如何改进以下preg_match_all模
我不知道为什么,但是当我执行脚本时,它没有正确显示这些行。它只是返回可变行的最后一行,下面没有输入。我是不是忽略了什么? 文件1: 06BAH TOI00123-11-134-B
我想检查 'n' 分解这个字符串: {$gallery#pager/collectionName/imageName/manual/no_effect/foo1/foo2/.../fooN} 到: v
我尝试先阅读其他问题,但没有发现任何有用的东西。 我在自己的 PC 上有一个装有 PHP 5.3.28 和 PHP 5.4.7 的开发服务器。 $string = 'Match this'; preg
我是一名优秀的程序员,十分优秀!