Reputation: 1291
Lets assume I have a string:
asdf/dfa/fds?adsf
I want to be able to get the string: asdf?adsf
So essentially I want to parse out everything from the first '/' to the '?'.
I tried experimenting with it (see example below) but I couldn't get it to work:
s = s.replaceAll("\\/([?]*)", "");
Upvotes: 2
Views: 62
Reputation: 83225
You should change it to this:
s = s.replaceAll("/.*(?=\\?)", "");
(?=\\?)
is a look-ahead that means "the next character is a literal ?
". (FYI, unescaped, ?
has special meaning in a regex.)
However, you did not mention what should happen if there are multiple ?
. The regex above will match until the last one.
If you want to match until the first,
s = s.replaceAll("/.*?(?=\\?)", "");
The extra ?
causes the repeated match to be "reluctant".
Upvotes: 2
Reputation: 124215
This should do the trick
replaceAll("/[^?]*", "")
/[^?]*
will match first /
and all non-?
characters after it.
Upvotes: 1
Reputation: 135992
try this
String s = "asdf/dfa/fds?adsf".replaceAll("/.*(?=\\?)", "");
Upvotes: 1