Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Regex Problem

by Anonymous Monk
on Mar 25, 2002 at 10:54 UTC ( #154064=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am not an expert on Regex, but I have found a problem I can't seem to solve. If there are any solutions please let me know. This is what I am trying to do:
while ($ts=~s/(<item name=\"DISPLAY_ORDER\"><value>)(\d+)(<\/value>< +\/item><item name=\"PARAGRAPH_TEXT\">)/$1$paranum$3/si) { print "Set PNum: $paranum\n"; $paranum++; }
which doesn't work. But when I insert a "," or "." at the end of the regex like so:
/$1$paranum,$2/si;) {
it works except it now displays a "," as well. I just want the $paranum inserted without the ",". Is there any obvious thing that I am doing wrong here?
Thanks for your help.

Replies are listed 'Best First'.
Re: Regex Problem
by Chady (Priest) on Mar 25, 2002 at 11:34 UTC

    seems like you're trying to parse XML? why don't you try something like XML::Simple

    or maybe this will work:
    while ($ts=~ s|(?<=<item name=\"DISPLAY_ORDER\"><value>)\d+(?=\<\/va +lue><\/item><item name=\"PARAGRAPH_TEXT\">)|$paranum|si ) { print "Set PNum: $paranum\n"; $paranum++; }
    (?<=... does a prematch, and (?=... does a postmatch.. and only changes the digit.
    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady |
Re: Regex Problem
by strat (Canon) on Mar 25, 2002 at 11:43 UTC
    Answering your question is rather difficult, since I don't know what your data looks like.

    Maybe something like .../$1.$paranum.$3/sie might help you...

    Btw: if you set paranum to an integer, you'll get an infinite loop...

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: Regex Problem
by Mudders (Initiate) on Mar 25, 2002 at 12:31 UTC
    Thanks for all the help...
    Sorry if I was a bit vague in my question. I am basically searching through a XML file for a value and replacing it with a new value. It was not as easy as I had origonally thought, but I have managed to get around the problem by adding a few ~~~~~ at $1$paranum~~~~~$3/si) {
    and then searching for $ts=~s/~~~~~//gis;
    after the loop. It is a bit of a hack but it serves my purpose for now.
    Thanks for all the help.
      wow. that's a bad, bad hack. you should not have to do that, and you should not do that! please, try XML::Simple. it's well documented, easy to use, and we'll be happy to help with any problems you might come across.

      ~Particle ;

Re: Regex Problem
by I0 (Priest) on Mar 25, 2002 at 17:37 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://154064]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-02-25 08:48 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.