vikas vikas - 28 days ago 10
Perl Question

Regex to get a combination of constant and pattern

I am working on a regex which can help me in replacing a pattern in string.

The string that I have in stream is very long and after applying the regex(Find the pattern and then replace with constant value) I have to carry forward the string into my ETL stream.

To find:
<customer attribute="any number">
like <customer attribute="1">
and replace with:
<customer>. (basically just keep "customer" and delete everything)


I am new to Regex and learning it.

Any help!!

Answer

Input:

<consumer attribute=\"1\"><birth-date>1990-07-23</birth-date> </consumer>;

my $element_name = "consumer";

my $str = "<consumer attribute=\"1\"><birth-date>1990-07-23</birth-date> </consumer>";

$str=~s/<($element_name)[^>]*attribute="[^\"]*"[^>]*>/<$1>/g;

print $str;

output:

<consumer><birth-date>1990-07-23</birth-date> </consumer>

Comments