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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex to uppercase tags 2

Status
Not open for further replies.

sucram

Programmer
May 23, 2001
75
0
0
GB
Hi,

I was wondering if anyone has come across some regex that could do the following, (I am terrible at regex).

Make everthing inside a html tag uppercase except what is enclosed in quotes.

From: <form action=&quot; method=&quot;post&quot;>

To: <FORM ACTION=&quot; METHOD=&quot;post&quot;>

Thanks,
Marcus
 
$tag ='<form action=&quot; method=&quot;post&quot;>';

print &quot;TAG = $tag\n&quot;;
$tag =~ s/(.*?)(&quot;.*?&quot;)/uc($1).$2/eg;
print &quot;TAG = $tag\n&quot;;

Output:
TAG = <form action=&quot; method=&quot;post&quot;>
TAG = <FORM ACTION=&quot; METHOD=&quot;post&quot;>

If you need to preserve the tag use a hold variable.

Just some notes about the RegEx:
The question marks make it not greedy so you won't run over all matches to get to the end of the string.
The s///g makes it iterate globally so you find them all.
The s///e makes the right side &quot;uc($1).$2&quot; evaluated as an expression instead of literal allowing us to upper case it.

This also assumes you'll always have tag=&quot;xyz&quot; format.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top