Reputation: 2209
I've got this bit of code to grab a url within a textarea. It has been working great until I tried a url with a '+'
in it.
Pattern pattern = Pattern.compile("(.*)(https?[://.0-9-?a-z=_#!A-Z]*)(.*)");
Matcher matcher = pattern.matcher(text);
So I tried puting \\+
and \\\\+
in my code but it did not work. So i did some googling and stack overflow problems kept mentioning this guy
Pattern.quote("+");
However, I am not sure how I implement that statement into what I currently have now. If that is even the way I want to go. But I'm assuming I need to do something like this...
String quote = Pattern.quote("+");
Pattern pattern = Pattern.compile("(.*)(https?[://.0-9-?a-z=_#!A-Z]*)(.*)");
Matcher matcher = pattern.matcher(text);
And then add the variable quote somewhere in the pattern? Please help! I just learned this stuff today I'm brand new to it! Thank you?
Upvotes: 2
Views: 1128
Reputation: 95784
(https?[://.0-9-?a-z=_#!A-Z]*)
Bear in mind that [
and ]
denote a class of characters, and that this means that any character within it will be included. [aegl]+
will match "age", "a", "e", g", "eagle", and "gaggle". It also means that a character listed twice (like /
) is completely redundant.
Pattern.quote is useful, but will only return the same string with a backslash preceding any special character. Pattern.quote("+")
will return \+
.
Because +
has no significance between square brackets, you should be able to put a +
unescaped within the square brackets. At that point you can also add a \\
if it makes you feel better.
Pattern pattern = Pattern.compile("(.*)(https?[:/.0-9-?a-z=_#!A-Z+]*)(.*)");
Pattern pattern = Pattern.compile("(.*)(https?[:/.0-9-?a-z=_#!A-Z\\+]*)(.*)");
See it here: http://fiddle.re/0780
Upvotes: 1
Reputation: 263943
just escape the quote with \
, example
Pattern pattern = Pattern.compile("(.*)(https?[://.0-9-?a-z=_#!A-Z\"]*)(.*)");
Upvotes: 3