简体   繁体   English

如何在正则表达式中将文本元素用作两个不同捕获组的一部分?

[英]How to use a text element as part of two different capture groups in regex?

I have a string I'm trying to parse with regex that seems to need one part of the string as a part of two different capture groups. 我正在尝试使用正则表达式解析一个字符串,该字符串似乎需要将字符串的一部分作为两个不同捕获组的一部分。 Consider the following example: 考虑以下示例:

<!-- 123 text -->text bit 1<!-- /123 text -->
<span class="someclass">something</span>
<!-- 456 text -->222 text<!-- /456 text -->

In this example code, I want three capture groups: 在此示例代码中,我需要三个捕获组:

  • everything up to (but not including) the 222 in the last line 直到(但不包括)最后一行中的222的所有内容
  • the 222 (or, the 222 plus its trailing space) 222(或222及其尾随空格)
  • the rest 其余的部分

The easiest unique piece of this is the --> followed by some number, so I thought of starting like this: 最简单的独特方法是-->后跟一些数字,因此我想到了从以下开始:

(.*-->\d+)

This would correctly find the right area, but I need that first capture group to be without the number and the second to have it. 这样可以正确找到正确的区域,但是我需要第一个捕获组没有编号,而第二个要有编号。 What am I missing? 我想念什么?

检查是否适合您

(.*-->)(\d+)(.*)

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

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