0

Regex: ^.*?(?=;)
Value: 00574/KVMK0224.jpg; 00574/1987432370PHANWHCO00MM.jpg
Now only matches: 00574/KVMK0224.jpg
Want: 00574/KVMK0224.jpg and 00574/1987432370PHANWHCO00MM.jpg

As I try to explain shortly I have a string with multiple image links, I made it working to get the first link, but now I want all links. I know how to use regex.Matches in C# to get multiple matches, the only thing I want to know is what regex to use for this.

What I have to get the first link:

    Regex regex = new Regex("^.*?(?=;)");
    Match match = regex.Match(link);
    if (match.Success)
    {
      part.ImageUrl = match.Value;
    }

What I made in order to get all links, I think everything is right on this exept of course the regex

    Regex regex = new Regex("^.*?(?=;)");
    foreach (Match match in regex.Matches(link))
    {
      list.Add(match.Value);
    }

Probably very simple to do this, but I don't have much experience with regexes.

Thanks in advance!

0

2 Answers 2

3

If all values are separated by ; then you don't need a regular expression. Try this:

string imagesString = "....";
string[] images = imagesString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

Edit: Here you have an alternativ that uses regular expressions and handles white-space:

string imagesText = "00574/KVMK0224.jpg; 00574/1987432370PHANWHCO00MM.jpg";
string[] images = Regex.Split(imagesText, @"\s*;\s*");

This will work with or without white-space before or after ;

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! this works great! Only one last question, all the links after the first will have a space before it, how to get rid of that? (I know how to do it, but maybe there is a smooth way like this?)
Just use the method .Trim() inside the foreach loop to get rid of extra white-space. PS: If my answer helped you, please mark it as the the correct answer. I'm glad I could help
You're welcome. I edited my answer and included an alternative using regular expressions.
Thanks for bringing up more ways to do this, Learning is never bad :)
1

Perhaps you can try this one ?

[\w/]*?.jpg(?=;)?

2 Comments

Thanks for your answer, but I prefer Rui Jarimba's way without using regexes. Really thanks for answering anyway!
ok no problem, i like the regex's "elegance" :) i learn how to exclude a suffix thanks to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.