0

I get an xml node;

<p:FirstAddressLine1></p:FirstAddressLine1>

I want to rewrite that node, if it has an empty string/null. I used ^$ but it fails to validate that particular xml node contains empty string.

Anyone knows , what im doing wrong here? and the right regular expression to be used?

MattSizzle
  • 3,145
  • 1
  • 22
  • 42
Ratha
  • 9,434
  • 17
  • 85
  • 163

1 Answers1

1

I would use something like the following regex

(?:<p:FirstAddressLine1>(?!<\/p))

Is uses a Negative Lookahead, and will match <p:FirstAddressLine1> followed by anything other then </p. If it sees a </p directly after the first <> it will not match the string.

EXAMPLE USAGE

use strict;
use warnings;

my @lines = <DATA>;

foreach (@lines) {

    if ( $_ =~ m/(?:<p:FirstAddressLine1>(?!<\/p))/ ) {
        print "The string is NOT empty\n";

    }
    else {
        print "The string is empty\n";
    }

}

__DATA__
<p:FirstAddressLine1></p:FirstAddressLine1>
<p:FirstAddressLine1>TEST</p:FirstAddressLine1>

RESULT

The string is empty
The string is NOT empty
MattSizzle
  • 3,145
  • 1
  • 22
  • 42