1

I'm really not great with RegEx in C#, never really used them but I have a long string that contains a lot of html that may contain numerous text parts like

src="Folder/Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

or

src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

I want to apply a reg ex over the string if it can be done in C# so it replaces the folder path so it will change any and all to be src = filename.extension

ie.

src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

becomes

src="cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

Can anyone please help?

RobboLew
  • 95
  • 8
  • 4
    So you just want to remove everything before the last forward slash, right? What about [`Path.GetFileName(string path)`](https://msdn.microsoft.com/en-us/library/system.io.path.getfilename%28v=vs.110%29.aspx)? – D Stanley Feb 01 '17 at 19:14
  • 1
    Have you tried any regex patterns that haven't worked? Will your HTML always follow the src="uploads/*.png pattern? Try to provide some more context to what you're looking to do along with what you've already tried to avoid a question appearing to be a plea for 'the codez'. – Josh Feb 01 '17 at 20:22
  • Binging HTML Regex returns a ton of content, including questions on this site: http://stackoverflow.com/questions/1028362/how-do-i-extract-html-img-sources-with-a-regular-expression http://stackoverflow.com/questions/4257359/regular-expression-to-get-the-src-of-images-in-c-sharp – Josh Feb 01 '17 at 20:24
  • "I want to apply a reg ex over the string ". So you have already parsed the HTML and have a list of strings which only have "src=..."? Correct? Or is this supposed parse the whole document? – ΩmegaMan Feb 01 '17 at 20:30

3 Answers3

1

RegEx for your replace:

src="Uploads/fd123051-532d-4804-a0fb-fd4ce6b70f7c/cd212dd7-7600-4b3f-a7d9-9a85c85a50ca.png"

Will be:

F: src="(.+?)//(.+?)//(.+?).png" [You can check "Dot Matches All"]

R: src="$1/$2/$3.png"  Or you can use instead of  $1 ,   /1 /2 /3 etc.
WIKIN
  • 13
  • 6
0

You can use:

src = Path.GetFileName(src);
0

You need substring function that will take only the part which you want from string Please go here.

Get file name from path

Community
  • 1
  • 1