Regex - Match sequence between lines that depend previous value

时间:2016-08-31 18:33:19

标签: regex xml notepad++

I tried make a regex to get inside "Miles" tag but only for the "Product1", but the most near that I arrived was this

(?<=<Product>Product1)[](?<=<Miles>)[\d\D]*?(?=<\/Miles>)

<ProductList>
    <Product>Product1</Product>
    <Dot/>
    <Miles>200</Miles>

    <Product>Product2</Product>
    <Dot/>
    <Miles>200</Miles>
</ProductList>

I don't know how can I take away the previous part before '' from the result.

Test: https://regex101.com/r/wU0iZ0/4

The application I'm using is notepad++

1 个答案:

答案 0 :(得分:-1)

This regex captures the content before miles for a given product in group 1, and the replacement puts in a new value. For example, to replace the miles for Product2 with 666:

Find: (<Product>Product2([\d\D](?!<Product>))*<Miles>)\d*
Repl: ${1}666

The curly brackets around the group number are needed to disambiguate the group number.

See live demo of the regex in action.