正则表达式根据 vqmod xml 的元素之一的属性查找 div

Regex to find a div based on the attribute of one of its elements for vqmod xml

好的,我正在为 opencart 创建一个 vqmod 扩展,我需要向管理页面添加一个字段。管理页面有一个包含 bootstrap 个元素的表单,如下所示:

<div class="form-group">
  <label class="col-sm-2 control-label" for="input-keyword"><span data-toggle="tooltip" title="<?php echo $help_keyword; ?>"><?php echo $entry_keyword; ?></span></label>
  <div class="col-sm-10">
    <input type="text" name="keyword" value="<?php echo $keyword; ?>" placeholder="<?php echo $entry_keyword; ?>" id="input-keyword" class="form-control" />
    <?php if ($error_keyword) { ?>
    <div class="text-danger"><?php echo $error_keyword; ?></div>
    <?php } ?>
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label" for="input-bottom"><span data-toggle="tooltip" title="<?php echo $help_bottom; ?>"><?php echo $entry_bottom; ?></span></label>
  <div class="col-sm-10">
    <div class="checkbox">
      <label>
        <?php if ($bottom) { ?>
        <input type="checkbox" name="bottom" value="1" checked="checked" id="input-bottom" />
        <?php } else { ?>
        <input type="checkbox" name="bottom" value="1" id="input-bottom" />
        <?php } ?>
        &nbsp; </label>
    </div>
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label" for="input-status"><?php echo $entry_status; ?></label>
  <div class="col-sm-10">
    <select name="status" id="input-status" class="form-control">
      <?php if ($status) { ?>
      <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
      <option value="0"><?php echo $text_disabled; ?></option>
      <?php } else { ?>
      <option value="1"><?php echo $text_enabled; ?></option>
      <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
      <?php } ?>
    </select>
  </div>
</div>

我需要告诉我的 vqmod XML 在 "input-bottom" 表单组之前添加一个表单组。我可以做这样的事情:

<search position="before"><![CDATA[<div class="form-group">
            <label class="col-sm-2 control-label" for="input-bottom"]]></search>

但这对我来说看起来很脆弱。如果更改标签 class 或其他任何内容,它就会中断。那么有没有一种方法可以使用正则表达式或其他东西在 "input-bottom" 标签之前找到 <div class="form-group">,而不管 div 和标签的 [=] 之间的 HTML 25=] 属性?

还是我看错了?有没有更好的方法来做我想做的事情?我是 opencart 和 vqmod 的新手。

所以我找到了这个 on Whosebug that helped me understand negated sets in lookaheads, and using the link in the same post, I tested this regex with this online tester:

<div class="form-group">(?=[^\>]*?bottom)

只要单词 "bottom" 出现在下一个 > 符号之前,它就会匹配 <div class="form-group">。这是我能想到的最好的结果了。

然后我在 vqmod 中尝试了一下,发现它不起作用...仔细检查 vqmod 的文档后,我了解到它只能搜索 一行.. .

但是使用 vqmod 中的 "offset" 属性,我能够做到这一点:

<search position="before" offset="1"><![CDATA[for="input-bottom"]]></search>
<add><![CDATA[my code]]></add>

它搜索具有 input-bottom 的行并将我的代码放在它上面 2 行,跳过 <div class="form-group"> 行。这对我有用。