SteveB
SteveB

Reputation: 1514

C# Regex to replace style HEIGHT tags

I need to replace a HEIGHT tag with MIN-HEIGHT but am struggling with excluding opening quotes from the replace.

This is my Regex:

[^-](height:)

This is my replace:

html = regex.Replace(html, "min-height:");

AS I understand it this should match 'height:' but not 'min-height:' but when I do the replace it also replaces the character immediately prior to height:.

so style="HEIGHT: becomes style=MIN-HEIGHT:

How do I maintain the opening quote in the replace but also cater for HEIGHT: being, say, the 3rd attribute of a style tag and not bumpered against the quotes (e.g. style="WIDTH: 200px; HEIGHT: 400px" ?

Example input would be:

style="HEIGHT: 500px;" style="WIDTH: 200px; HEIGHT: 500px" style="WIDTH: 200px;HEIGHT: 500px"

Expected output:

style="MIN-HEIGHT: 500px;" style="WIDTH: 200px; MIN-HEIGHT: 500px" style="WIDTH: 200px;MIN-HEIGHT: 500px"

Current output:

style=MIN-HEIGHT 500px;" style="WIDTH: 200px;MIN-HEIGHT 500px" style="WIDTH: 200pxMIN-HEIGHT 500px"

Regex101 link here

Upvotes: 1

Views: 774

Answers (1)

vks
vks

Reputation: 67988

(?<!-)(height:)

Try this.Use 0 width assertion rather than capturing .See demo.The lookbehind will make sure there is no - behind height being captured.Use i modifier if height is case-sensitive

https://regex101.com/r/vD5iH9/78

Upvotes: 4

Related Questions