Reputation: 300
I have the strings:
background: url('../../footer/right.png') repeat;
background: url("../../footer/left.png") repeat;
And i want to extract just the URL like this:
../../footer/right.png
../../footer/left.png
My RegExps so far are
.*?[(\'|(\"|(|\'|\"](.*?)[\"|\'|)|\")|\')].*?
[[\\(\']([^\']*?)[\'\\)]]|[\\(\"]([^\"]*?)[\"\\)]|[\\(]([^)]*?)[\\)]|[\']([^\']*?)[\']|[\"]([^\"]*?)[\"]
But it keeps given me
background: url../../footer/izquierdo.png repeat;
Why it does this and what´s the correct Pattern?
Edit
I have come to the answer with your help, but i don´t quite know why it works
(?<=\\(|\'|\")(.*?)(?=\'|\"|\\))
Any one know why it can match any combinations of (
("
('
'
"
?
And BTW, this matchs are correct for my logic (I don't mention it early but they do)
Thanks everyone for the help
Upvotes: 1
Views: 70
Reputation: 31689
Looking at this part of your regex:
[('|("|(|'|"]
(I've eliminated the backslashes that you need inside a Java string literal.) When you have a set of characters enclosed by []
in a regex, the effect is to match one character that must be in the set. Thus, [abc]
matches one character that can either be 'a'
, 'b'
, or 'c'
. It never matches a sequence of more than one character. There are other features, for example using ^
as the first character means match a character not in the set, and there are ways to specify entire character classes such as \d
for digit.
However, it appears that you're trying to use it to match a multiple-character sequence, i.e. you're looking for ('
or ("
in the string. You can't use a []
construct for that. The way you've written it, it will match one character that is either (
, '
, |
, or "
.
Upvotes: 0
Reputation: 2445
Try this:
url\([\'|\"](.+)[\'|\"]\)
as Java string will be:
"url\\([\\'|\\\"](.+)[\\'|\\\"]\\)"
Upvotes: 0
Reputation: 9644
You can use (adding escape for the quote where you need them)
(?|'([^']*)'|"([^"]*)")
Your answer will be in the first captured group (thanks to (?|...|...)
, used to reset the capturing groups index)
Upvotes: 0
Reputation: 124225
How about using something like
\\(('|\")(.*?)\\1\\)
or maybe even add url
before
url\\(('|\")(.*?)\\1\\)
\\1
represents match from group 1, which can be only '
or "
. Point in using it here is to make sure that we have same kind of quote after and before parenthesis.
This regex will place ../../footer/right.png
part in group 2, so you can just get it using matcher.group(2)
.
Example:
String data = "background: url('../../footer/right.png') repeat;\r\n" +
"\r\n" +
"background: url(\"../../footer/left.png\") repeat;";
Pattern p = Pattern.compile("\\(('|\")(.*?)\\1\\)");
Matcher m = p.matcher(data);
while (m.find())
System.out.println(m.group(2));
Output:
../../footer/right.png
../../footer/left.png
Upvotes: 1
Reputation: 6527
Try with String.split()
String urlStr = "background: url('../../footer/right.png') repeat;";
String url = urlStr.split("'")[1];
System.out.println(url);
output
../../footer/right.png
Upvotes: 0