2

I'm looking for a single line regex which does the following:

Given a HTML tag with the "name" attribute, I want to replace it with my own attribute. If that tag lacks the name attribute, I want to implant my own attribute. The result should look like this:

<IMG name="img1" ...> => <IMG name="myImg1" ...>
<IMG ...> => <IMG name="myImg1" ...>

Can this be done with a single line regex?

2
  • Which regex are you using Perl, Java, awk, etc? Commented Dec 11, 2008 at 14:56
  • Perl. But Python/awk/egrep would be just as good. Commented Dec 11, 2008 at 15:14

3 Answers 3

4

The trick is to match every complete "attribute=value" pair, but capture only the ones whose attribute name isn't "name". Then plug in your own "name" attribute along with all the captured ones.

s/<IMG
  ((?:\s+(?!name\b)\w+="[^"]+")*)
  (?:\s+name="[^"]+")?
  ((?:\s+(?!name\b)\w+="[^"]+")*)
  >
 /<IMG name="myName"$1$2>
 /xg;
Sign up to request clarification or add additional context in comments.

2 Comments

This is a better solution than mine, but it does move the name attribute to the start if it exists later in the tag.
Is that a problem? AFAIK, the order of the attributes doesn't matter.
1

This isn't a perfect solution, the spacing and position within the tag may not be exactly what you want, but it does accomplish the goals. This is with a perl regex, but there's nothing particular perl-specific about it.

s/(&lt;IMG)((\s+[^&gt;]*)name="[^"]*")?(.*)/$1$3 name="myID"$4/g

2 Comments

This almost works. It fails when the "name" tag isn't the first one.
That is untrue. Did you test it? the (\s+[^>]) covers preceding attributes.
0

If, like in your example, the name attribute is always the first one inside the IMG tag, then it's very easy. Search for

<(?!/)(/w+)\s+(name="[^"]+")?

and replace with

<\1 name="myImg1"

but I doubt that this is what you really want.

If the name attribute can occur in other positions, it gets more difficult.

1 Comment

Well, since I edit general HTML files, I cannot be sure the attribute is the first one. Actually, I'm quite sure it's not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.