Reputation: 1087
What I am trying is to extract my account id from a url for other validations. see my URL samples.
http://localhost:8024/accounts/u8m21ercgelj/
http://localhost:8024/accounts/u8m21ercgelj
http://localhost:8024/accounts/u8m21ercgelj/users?
What I required is to extract u8m21ercgelj from the url. I tried it with below code but it fails for the cases like http://localhost:8024/accounts/u8m21ercgelj
i.e with out a / at the end.
public String extractAccountIdFromURL(String url) {
String accountId = null;
if ( url.contains("accounts")) {
Pattern pattern = Pattern.compile("[accounts]/(.*?)/");
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
accountId = matcher.group(1);
}
}
return accountId;
}
Can any one help me?
Upvotes: 4
Views: 684
Reputation: 124225
[accounts]
doesn't try to find accounts
word, but one character which is either a
, c
(repetition of character doesn't change anything), o
, u
, n
, t
or s
because [...]
is character class. So get rid of those [
and ]
and replace them with /
since you most likely don't want to accept cases like /specialaccounts/
but only /accounts/
.
It looks like you just want to find next non-/ section after /accounts/
. In that case you can just use /accounts/([^/]+)
If you are sure that there will be only one /accounts/
section in URL you can (and for more readable code should) change your while
to if
or even conditional operator. Also there is no need for contains("/accounts/")
since it just adds additional traversing over entire string which can be done in find()
.
It doesn't look like your method is using any data held by your class (any fields) so it could be static.
Demo:
//we should resuse once compiled regex, there is no point in compiling it many times
private static Pattern pattern = Pattern.compile("/accounts/([^/]+)");
public static String extractAccountIdFromURL(String url) {
Matcher matcher = pattern.matcher(url);
return matcher.find() ? matcher.group(1) : null;
}
public static void main(java.lang.String[] args) throws Exception {
String examples =
"http://localhost:8024/accounts/u8m21ercgelj/\r\n" +
"http://localhost:8024/accounts/u8m21ercgelj\r\n" +
"http://localhost:8024/accounts/u8m21ercgelj/users?";
for (String url : examples.split("\\R")){// split on line separator like `\r\n`
System.out.println(extractAccountIdFromURL(url));
}
}
Output:
u8m21ercgelj
u8m21ercgelj
u8m21ercgelj
Upvotes: 4
Reputation: 650
Your regex is written as such that it is expecting to receive a trailing slash - that's what the slash after the (.*?)
means.
You should change this so that it can accept either the trailing slash, or the end of the string. (/|$)
should work in this case, meaning your regex would be [accounts]/(.*?)(/|$)
Upvotes: 3