views:

54

answers:

4

Hi, I want a regular expression that could be used to find the following lines:

<rect width='10px' height ='20px'/>
<rect width='20px' height ='22px'/>
<circle radius='20px' height ='22px'/>

and replace them by the these lines:

<rect width='10px' height ='20px'></rect>
<rect width='20px' height ='22px'></rect>
<circle radius='20px' height ='22px'></circle>

Thank you .

+1  A: 

Like polygenelubricants noted I don't know what this would accomplish but this should be what you are looking for:

<rect[^>]*/>

and

<circle[^>]*/>

If you want to match any self-contained tag you should look at Crozins solution.

marg
+1  A: 

You could use something like this #<([a-z]+)([^>]*)/># and replace with <$1$2></$1>. But regexp might differ depending on what's regexp engine you're using.

Crozin
+1  A: 

sed 's/<\([a-z]*\) \([^\/>]*\)\/>/<\1 \2><\/\1>/'

Would do what you want (in this case)

Search pattern: <\([a-z]*\) \([^\/>]*\)\/>

Replace pattern: <\1 \2><\/\1>

Jamie Wong
+1  A: 

I don't think regex is the right tool for this job, but something like this will "work" some of the time.

    String text =
        " <rect width='10px' height ='20px'/> \n" +
        " <rect width='20px' height ='22px'/> \n" +
        " <circle radius='20px' height ='22px'/> \n" +
        " <square/> <rectangle></rectangle> \n" +
        " <foo @!(*#&^#@/> <bar (!@*&(*@!#> </whatever>";
    System.out.println(
        text.replaceAll("<([a-z]+)([^>]*)/>", "<$1$2></$1>")
    );

The above Java snippet prints:

 <rect width='10px' height ='20px'></rect> 
 <rect width='20px' height ='22px'></rect> 
 <circle radius='20px' height ='22px'></circle> 
 <square></square> <rectangle></rectangle> 
 <foo @!(*#&^#@></foo> <bar (!@*&(*@!#> </whatever>

The regex is this (see also on rubular.com):

/<([a-z]+)([^>]*)\/>/

Essentially we try to capture what we hope is a tag name in group 1, and everything else until the /> in group 2, and use these captured strings in our substitution.

References

polygenelubricants