prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

i am new to perl and i am having a doubt in a regex.

The text looks as follows

<PrimaryIE id="10001">organisation structure<pg>2.3, 4.5, 6.8</pg></PrimaryIE>

I want to get the pg tag contents and some more informations from the above text. i want to write a regular expression using negative look ahead assertion for the above case

i wrote the regex like the one that follows. is this right or not

$str =~ /<PrimaryIE id="[^\"]+">.*(?!<pg>)<pg>(.*?)<\/pg><\/PrimaryIE>/gsi

20040701 Edit by castaway: Changed title from 'regex'

Replies are listed 'Best First'.
Re: regex using negative look ahead assertion
by diotalevi (Canon) on Jun 26, 2004 at 11:38 UTC

    This is better handled with XPath.

    m( <pg [^>]* > # A <pg> tag with optional body ( # Capture START (?: # Skip past all the non-tag-beginng-like things but be sure # not to allow backtracking. (?> [^<]+ ) | # OR # Match something like a tag beginning as long as it # isn't an opening for <pg < (?! pg\s ) ) ) # Capture END </pg> )x;