Recurring pattern matching Perl


Hi,

I have a XML text like the one below. I'd like to search all instances of the <MYTAG1> tag and get the data inside. I'm looking for a quick regex solution, not an XML parser.

something like this - although this does not seem to work. It only gives me the last instance of the tag....
while ($xmltext =~ m/.*<MYTAG1>(.*)<\MYTAG1>.*/g) {
    #  do something
}

XML text:
            <MYTAG1>somedata</MYTAG1>
            <MYTAG2>somedata</MYTAG2>
            <MYTAG1>somedata</MYTAG1>
            <MYTAG3>somedata</MYTAG3>
            <MYTAG2>somedata</MYTAG2>
            <MYTAG1>somedata</MYTAG1>

php-newbieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
Provided you don't need to later use the data in $xmltext, this will work:
while ($xmltext =~ m{<MYTAG1>(.*?)</MYTAG1>}) {
    # do something with $1
}

Open in new window

0
ozoCommented:
$xmltext=q<
           <MYTAG1>somedata</MYTAG1>
            <MYTAG2>somedata</MYTAG2>
            <MYTAG1>somedata</MYTAG1>
            <MYTAG3>somedata</MYTAG3>
            <MYTAG2>somedata</MYTAG2>
            <MYTAG1>somedata</MYTAG1>
>;
while( $xmltext =~ m/<MYTAG1>(.*?)<\/MYTAG1>/g ){
  print "do something with $1\n"
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
try:
while ($xmltext =~ m{<(MYTAG\d)>([^<]*?)</$1>}) {
    # do something with $2
}

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

php-newbieAuthor Commented:
Wilcoxo, Thanks for your tip. When I use the code as you pasted it I get an endless loop. But this works for me:

      while ($body =~ m/<MYTAG1>(.*?)<\/MYTAG1>/g) {
            print $1."\n";
      }
0
hieloCommented:
If you want to extract all the tags use:
while ($xmltext =~ m{<(MYTAG\d)>([^<]*?)</\1>}) {
    # do something with $2
 print "$2";
}

If you want MYTAG1 specifically then on the above replace \d with 1
0
ozoCommented:
@MYTAG1 = m/<MYTAG1>(.*?)<\/MYTAG1>/g;
dosomething foreach @MYTAG1;
0
wilcoxonCommented:
Good point.  Mine was supposed to be destructive ($xmltext =~ s{<MYTAG1>(.*?)</MYTAG1>}{}) but ozo's answer (either one) is better - I rarely do this sort of looping so forgot g modifier will loop in scalar context.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.