简体   繁体   English

正则表达式 - 匹配中的匹配

[英]Regular Expressions - Match inside a Match

I am trying to find a way to match something inside a match.我正在尝试找到一种方法来匹配比赛中的某些东西。 I am still reading oreilly's book on regular expressions, but this is something I have to do now:我仍在阅读 oreilly 关于正则表达式的书,但这是我现在必须做的事情:

I need to get all the values from the inputs inside a form.我需要从表单内的输入中获取所有值。 The page contains multiple forms, so normally I would do something similar to this:该页面包含多个 forms,所以通常我会做类似这样的事情:

type="checkbox" value="([^"])"

But now I need to target all the checkbox values inside the specific form.但现在我需要针对特定表单内的所有复选框值。 Using preg_match by the way.顺便使用 preg_match 。

ps I know I could get the form with preg_match first, and the match inside that result, but I'm wondering how to do this in regex. ps 我知道我可以先获得带有 preg_match 的表格,然后再获得该结果中的匹配项,但我想知道如何在正则表达式中执行此操作。

preg_match("|type=\"checkbox\" value=\"(.*?)\"|");

You really shouldn't be using regular expressions to parse HTML.你真的不应该使用正则表达式来解析 HTML。 Look into the HTML DOM Parser .查看HTML DOM Parser Using this your code becomes very simple.使用它,您的代码变得非常简单。

$html = file_get_html('http://www.website.com/');

$form = $html->find('#formId')

foreach($form->find('input[type=checkbox]') as $element) {
       //Do what you need to do
}

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

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