0

I am very new to coffee and regex, I am trying to do some string manipulation

I will have a string like this

x = '<div class="sample">
         <img src="http://www.example.com/i/java.png"> 
     </div>
     <div class="sample_another">
         <img src="/i/somedir/python.png" height="200px"> 
    </div>'

Now I want to parse the the url of the img tag and get the height and width of image so the string after manipulation should look something like this. also This manipulation should not be done, if the image tag already has the height or width or both defined.

x = '<div class="sample">
         <img src="http://www.example.com/i/java.png" height="200px" width="100px" > 
     </div>
     <div class="sample_another">
         <img src="/i/somedir/python.png" height="200px"> 
    </div>'

Any help on how to achive this? thanks

Pujan
  • 69
  • 1
  • 12
  • Is the `width` property of the picture always 1/2 of the height? Is the string always two elements with the second always having at least a `height` property? – eqwert Jul 20 '17 at 21:07
  • ohh no width and height does not have any relation and are variable. sstring could be variable as well – Pujan Jul 20 '17 at 21:34
  • HTML parsing in Regex [isn't fun](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). You might want to consider another way to go about this problem. – eqwert Jul 20 '17 at 21:42
  • i couldn't find any other option :( trying to generate a regex myself as of now. Any idea you can think of ? – Pujan Jul 20 '17 at 22:07

0 Answers0