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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:



May 19, 2016, 7:18 AM

Post #1 of 2 (1293 views)
PERL--REGEX Can't Post

I am working on the following xml sample.

<one>line 1one</one>
<one>line 2two
<two>child 2one</two>
<three>child 2two</three>

<one>line 3three</one>

I do understand that using a module to parse an XML file is recommended.

I am interested to see if a regex in PERL can be developed to produce the following output


<one>line 3three</one>


Output is produced by taking the following conditions into account.

If the value(attribute) between the elements contain, "one", remove those elements.
If the value(attribute) "one", exists, in the child elements of a parent element, remove all the child elements along with the parent element.

In this example:
parent element---->one
child element---->two
child element---->three

Can a regex be created to generate the required output !

Veteran / Moderator

May 19, 2016, 11:19 PM

Post #2 of 2 (1285 views)
Re: [cognizant] PERL--REGEX [In reply to] Can't Post

Nope. Even for simpler cases, a regex is simply not the right tool. Here, it is a typical example where you just can't do it with a regex. Well, you could possibly build a complicated regex that would work for that very specific case, but you would have to make many assumptions about the exact format, so that the regex would break if anything is slightly different or for more general case.

Don't try to do that, you'd be bound to failure.

Even using several regexes would not save your day, or you would end up building yourself a clunky and unpractical mini-parser, use a parsing module for that.


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

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