in reply to regex - need first child of parent
/<para0[^>]*?> (?: \s* (?: <title .*?<\/title> |<para .*?<\/para> |<applic .*?<\/applic> |<capgrp .*?<\/capgrp> |<subpara1 .*?<\/subpara1> |<caution .*?<\/caution> ) )*? \s* (<warning .*? <\/warning>) (?: \s* (?: <title .*? <\/title> |<para .*? <\/para> |<applic .*? <\/applic> |<capgrp .*? <\/capgrp> |<subpara1 .*? <\/subpara1> |<caution .*? <\/caution> |<warning .*? <\/warning> ) )*? \s* <\/para0> /sx
The code works as follows:
Note that your entire XML must be in a single string (not an array) and should be executed with the /s modifier.
Update: Some additional notes. This assumes that your XML is well-formed. It assumes you have at least one warning element in your para0 element. And most importantly, if there are 1st generation tags which are not accounted for, .*? can jump to unexpected locations, meaning you will not get what you expected. Compare that to ikegami's solution, which will just work.
Update 2: At shmem's suggestion, I added an /x modifier and reformatted the regex to make it (maybe) easier to follow.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex - need first child of parent
by kdolan (Initiate) on Mar 11, 2009 at 18:37 UTC |