Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Regex problem while parsing tagged, hierarchical data

by mk. (Friar)
on Sep 12, 2006 at 15:10 UTC ( #572556=note: print w/replies, xml ) Need Help??


in reply to Regex problem while parsing tagged, hierarchical data

hope this helps:
perl -i -pe 'if (/<level1 id=\"(.*)\">/) {$currentid = $1;} elsif (/<level2>/) {s/<level2>/<level2 id="$currentid">/g}' input

edit: just deleting the redundant $_.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"one who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever."

mk at rio dot pm dot org

Replies are listed 'Best First'.
Re^2: Regex problem while parsing tagged, hierarchical data
by Mago (Parson) on Sep 19, 2006 at 11:25 UTC
    perl -pi -e 'if(/<level1(\sid=\".*\")>/){$a=$1};if(/<level2>/){$_=~s/( +<level2)>/$1$a>/g}' input or perl -pi -e '$a=m/(<level([12]))(\sid=\".*\")*>/;if($a){$2==1?$b=$3:$_ +=~s/($1)/$1$b/g;}' input or perl -pi -e '$a=m/(<level(1|2))(\sid=\".*\")*>/;$a?$2==1?$b=$3:$_=~s/( +$1)/$1$b/g:next;' input or perl -pi -e '$a=m;(<level(1|2))(\sid=\".*\")*>;;$a?$2==1?$b=$3:$_=~s;( +$1);$1$b;g:1;' input
    or golf by monsieur_champs
    perl -pi -e 'm;(<level(1|2))(\sid=\".*\")*>;;$&?$2==1?$b=$3:$_=~s;($1) +;$1$b;g:1;' input
    sorry =) -1 char
    perl -pi -e 'm;(<level(1|2))(\sid=\".*\")*>;;$&?$2==1?$b=$3:$_=~s;($1) +;$1$b;g:1' input
    insane !!! 33 chars
    perl -pi -e '/l1(\sid=.*)>/?$a=$1:s;l2>;l2$a>;' input

    =)


    Mago
    mago@rio.pm.org


      perl -pi -e '/(<level(1|2))(\sid=\".*\")*>/;$&?$2==1?$b=$3:$_=~s;($1);$1$b;g:1;' input
        Ups !! -1 char
        perl -pi -e '/(<level(1|2))(\sid=\".*\")*>/;$&?$2==1?$b=$3:$_=~s;($1); +$1$b;g:1' input


        Mago
        mago@rio.pm.org


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2022-01-27 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (70 votes). Check out past polls.

    Notices?