Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non greedy pattern matching

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
EU
I'm trying to work on xml without loading the xml parsing bits. Here's what I've got so far
Code:
#!/usr/bin/perl -w
use strict;

my $string2 = '
<node description>
  <hook>
    <text> Blurb about Red Hat
    </text>
  </hook>
</node>
<node second node>
  <hook>hooky AIX thing
  </hook>
</node>
<node third node>
  <hook>hooky solaris thing
  </hook>
</node>';
$string2 =~ s/\<node.*?Solaris.*?\<\/node\>/replacement /msi;
print $string2;
Now, according to me this should just take out the third solaris node but the result is the whole thing is deleted.

I've tried looking everywhere and I'm assured that the .*? pattern is non gready list of any characters. What am I doing wrong?


On the internet no one knows you're a dog

Columb Healy
 
You should raise and answer the question: is the whole string being captured by that regex? The answer is yes, as it starts with [tt]<node[/tt] , continues with some chars, has the word [tt]solaris[/tt] and ends with [tt]</node>[/tt].
The point is that any number of chars means exactly what it says.
Try this
[tt]$string2 =~ s/<node.*>\s*<hook>[\w\s]*Solaris[\w\s]*<\/hook>\s*<\/node>/replacement /i;[/tt]
but other solutions also exist.
Note that modifiers [tt]m[/tt] and [tt]s[/tt] are somewhat in contrast each other and [tt]m[/tt] is of no use if '^' or '$' are not in the pattern.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top