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?

4
  • 4
    So you just want to remove everything before the last forward slash, right? What about Path.GetFileName(string path)? Commented Feb 1, 2017 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'. Commented Feb 1, 2017 at 20:22
  • Binging HTML Regex returns a ton of content, including questions on this site: stackoverflow.com/questions/1028362/… stackoverflow.com/questions/4257359/… Commented Feb 1, 2017 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? Commented Feb 1, 2017 at 20:30

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

Comments

0

You can use:

src = Path.GetFileName(src);

Comments

0

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

Get file name from path

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.