简体   繁体   中英

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
  • the 222 (or, the 222 plus its trailing space)
  • 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+)(.*)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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