I want to capture both values after <ParagraphB>
, but I'm currently stuck on only capturing the last match. Here is my regex so far:
<ParagraphB>(?s).*<value>([^\<]*)
<ParagraphA>
<value>12</value>
<ParagraphB>
<value>34</value>
<value>56</value>
Any help is appreciated.
To capture the value multiple times, we need to use the global
flag and retrieve the captured group in each match.
Demo: https://regex101.com/r/1Demzp/1
Pattern: (?:<ParagraphB>|\G<\/value>)\s*?<value>([^<]*)
Explanation: For the first time match, <ParagraphB>
. From next iteration, only match if the string starts with </value>
(?:<ParagraphB>|\G<\/value>)
: Match <ParagraphB>
or string should start with </value>
. \G
is for continuing matches.\s*
: Non greedy match of any no of space characters([^<]*)
: Capture all characters till <
.