0

I am trying to remove only one property, height, and its value, from an inline style. I would like to start with this:

<img src="http://hello-world/img/a1.jpg" style="width: 800px; height: 600px; float:right;">

And make it like this:

<img src="http://hello-world/img/a1.jpg" style="width: 800px; float:right;">

I am using Java 7 environment and how can I solve this problem using Pattern.compile?

  • [Parsing HTML with regex is a hard job](https://stackoverflow.com/a/4234491/372239) HTML and regex are not good friends. Use a parser, it is simpler, faster and much more maintainable. – Toto Aug 10 '20 at 14:53

1 Answers1

0

I suggest following regex to detect property height and his value.

(<.*?style=.*?)(\s*height.*?;)(.*?>)

Details:

  • <.*?style=.*?: group1 - from tag html to style inline, before property height
  • \s*height.*?;: group2 - property height and his value (any unit)
  • .*?>: group 3 - rest of the tag html

Prove

Finally, you keep only group 1 and group 3 ("$1$3"). Using Java Regular Expression Tester, you test result.

Thân LƯƠNG Đình
  • 3,082
  • 2
  • 11
  • 21
  • +1 Thanks alot, it will help me. I have one more question. I have tried deleting not only the property `height` but also the property `width` as follows. Do you know a better way than this? `(<.*?style\s*=.*?)(\s*[width|height].*?;)(\s*[width|height].*?;)(.*?>)` – user2335816 Aug 13 '20 at 04:56
  • @user2335816 I suggest my solution. `(<.*?style\s*=.*?)(\s*(?:width|height).*?;)(.*?)((?2))(.*?>)` Details: - Please use character ( instead of [ - `(?2)`: Repeat pattern of group 2 https://regex101.com/r/uL7TAS/3 – Thân LƯƠNG Đình Aug 13 '20 at 15:20