CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [hurz] XML RegEx Problem: Edit Log



BillKSmith
Veteran

Jun 20, 2014, 5:38 AM


Views: 47013
Re: [hurz] XML RegEx Problem

I cannot think of a way to do the whole job with one regex. (It seems to require a variable length look-ahead assertion which is not available in perl) Another approach is to use a regex to extract each Name and all the text that goes 'with' it. Use a second regex to extract the Token(s) from each block of text.

UPDATE: Better Solution

An even better solution is to use perl's IO to break your input into logical blocks:


Code
use strict; 
use warnings;
local $/ = '<Test>';
<DATA>;
while (<DATA>) {
my ($name) = /(.+)\<\/Test\>/;
my @tokens = /\{(.+)\}/g;
print "$name: @tokens\n";
}

__DATA__
<Test>Name1</Test>
<a>
<b>Something {Token1}</b>
</a>
<c>
</c>
<a>
<b>Something {Token2}</b>
</a>
<c>
</c>
<Test>Name2</Test>
<Test>Name3</Test>
<a>
<b>Something {Token3}</b>
</a>


Output

Code
Name1: Token1 Token2 
Name2:
Name3: Token3

Good Luck,
Bill

(This post was edited by BillKSmith on Jun 20, 2014, 7:53 AM)


Edit Log:
Post edited by BillKSmith (Veteran) on Jun 20, 2014, 7:53 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives