1

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 .

Abdullah
  • 5,445
  • 10
  • 41
  • 48
  • 1
    What would this acccomplish, really? – polygenelubricants Jun 26 '10 at 17:35
  • 3
    I think if you desperately want that, read the XML with a proper parser and output it again after telling the parser not to use self-closing tags. – Joey Jun 26 '10 at 17:37
  • 2
    I know this one is linked too much, but I think in this case it's appropriate: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Kobi Jun 26 '10 at 17:38
  • @poly: I use svg with "jquery svg" , it does not work if I write "/>" instead of "". I know it's the same. but jquery does not. @Rossel: Thanks,it's a good idea. – Abdullah Jun 26 '10 at 17:41
  • so is this JavaScript, or server-side? What flavor are you using? – Kobi Jun 26 '10 at 17:42

4 Answers4

1

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
  • 2,817
  • 1
  • 31
  • 34
1

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
  • 43,890
  • 13
  • 88
  • 135
1

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
  • 18,104
  • 8
  • 63
  • 81
1

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
  • 376,812
  • 128
  • 561
  • 623