Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Regex problem while parsing tagged, hierarchical data

by prasadbabu (Prior)
on Sep 12, 2006 at 15:03 UTC ( [id://572551]=note: print w/replies, xml ) Need Help??


in reply to Regex problem while parsing tagged, hierarchical data

Sivaraman,

I can see couple of mistakes in your code. I don't know why you are using '*?' when you are already using negative look behind. Also you are matching only the first <level2> from <level1> in your coding, so it is not matching.

Also it is good to use Parser for these kind of works like jdtoronto and Fletch suggested.

Though not efficient, here is my try in regex to match all <level2> inside <level1>

$content =~s#(<level1 id=\"([^"]*)\"(?:(?!(?:<level1|$)).)*)(<level1)?#my $id = $2; my $level1 = $1; my $rest = $3; ($level1 =~ s|<level2>|<level2 id="$id">|g);$level1.$rest#egsi;

update: sivaraman, immediately, i added '?' in (<level1)? after i posted. Sorry for not mentioning the updation in the node.

Prasad

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://572551]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-03-29 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found