Ahoy,
I have a problem, see; I have strings like:
<img width="594" height="392" src="/sites/it_kb/SiteAssets/Pages/exploding%20the%20VDI%20vDesktop/VDI3.PNG" alt="" style="margin:5px;width:619px;height:232px" />
They are not consistently formatted.
I need to parse strings like this, and return the following:
<img width="594" height="392" src="/exploding%20the%20VDI%20vDesktop-VDI3.PNG" alt="" style="margin:5px;width:619px;height:232px" />
Changes:
- Remove everything except the immediate directory in which the image file lay.
- Instead of that directory being a subdirectory, prepend it onto the file name.
So if the file is currently in /blabla/bla/blaaaaah/pickles/pickle.png
then I want the IMG SRC attribute to say pickles-pickle.png
Now, I've been trying to do this with regex, but after 3 hours, I've discovered something about myself... I am awful at regex. I could be at this for weeks, and I'd never get anywhere.
Thus, I am asking this wonderful community for two things:
- How would you do this? Is regex even the right answer? I need to be able to parse any SRC attributes inside IMG tags (whether or not they have height/width or other attributes).
- What resources would you recommend for me to learn regex with .NET?
Now for the problem at hand, I suppose I could do a string.replace where I....
- Find the IMG tag, and get indexes of the surrounding '<' and '>'
- Find index of 'SRC=' and ' ' (space) between those two instances
- Find last index of '/' between the src and space indexes
- Find second to last index of '/' between src and space indexes
- Replace... er no, remove... everything before the second to last instance of '/'...
- ...String.Replace remaining '/' with '-'.
- ....I.. I think that'd do it?
But DAMN that is ugly. A regex would be so much prettier, don't you think?
Any advice?
Note: I tagged this as 'homework', but it's not homework. I'm volunteering for work after-hours to save the company like 200k. This is literally the last piece of an incredibly convoluted (to me) puzzle. Of course, I don't see a penny of that 200k, but I look good doing it.