2

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?

2 Answers 2

3

just escape the quote with \, example

Pattern pattern = Pattern.compile("(.*)(https?[://.0-9-?a-z=_#!A-Z\"]*)(.*)");
Sign up to request clarification or add additional context in comments.

5 Comments

remove the // too. a) it should be \\, and b) you don't need to escape dots within a character class
like this? Pattern pattern = Pattern.compile("(.*)(https?[://.0-9-?a-z=_#!A-Z\"quote\"]*)(.*)");
@Bohemian, i don't get it. why would you remove //? i think it didn't escape dots.
Unfortunately it is not escaping the +'s for me with the new code. Are you sure I just put the string into the pattern?
@gmustudent how about this? (.*)(https?[://.0-9-?\\+a-z=_#!A-Z\"]*)(.*)
1
(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

2 Comments

Thank you so much for the explanation. It cleared up a lot of things for me and I learned quite a bit. unfortunately this seems to be working great in the console but breaks down in my web app every time. It just hates the + sign for some reason and won't read it I don't understand why.
@gmustudent Sounds tricky. Maybe I can help you in a SO chat room.

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.