No need for a regex, just
- Split the string with
'
substring
- Find the element starting with
http
- Return the first found item.
C# demo:
var s = "background-image: url('https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg')";
var res = s.Split(new[] {"'"}, StringSplitOptions.None)
.Where(v => v.StartsWith("http"))
.FirstOrDefault();
Console.WriteLine(res);
// => https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg
If you need to use a regex, use the standard regex to match a string between two strings, start(.*?)end
where (.*?)
captures into Group 1 any 0 or more chars other than a newline, as few as possible as the *?
quantifier is lazy:
var s = "background-image: url('https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg')";
var res = Regex.Match(s, @"'(.*?)'").Groups[1].Value ?? string.Empty;
Console.WriteLine(res);
// => https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg
See another C# demo