Dec 27, 2015, 6:21 AM
Post #1 of 5
Regex, Extract between tags, multiple times
I use the below code to extract text in a string from between the tags:
$string = "<result>101</result>";
$string =~ s~<result\>(.*?)\</result\>~~isg;
$found = $1;
$found in this case would be "101".
I now have a string that contains multiples of the same tag, so:
$string = "<result>101</result><result>202</result><result>303</result>";
Using the same regex, I will only ever retrieve the last entry. I want all 3.
I tried putting them in array, so:
@all = $string =~ s~<result\>(.*?)\</result\>~~isg;
$all gives the last found value ($1).
$all gives the number of occurances.
How can find the three values? I would like the 101, 202, 303 to each be an element in an array, or be in a string such as $string = "101|102|103";
Regex tends to drive me mad, lol.