Regular expression extracting string from url
I am trying to extract my account id from url for other checks. see my sample urls.
http://localhost:8024/accounts/u8m21ercgelj/
http://localhost:8024/accounts/u8m21ercgelj
http://localhost:8024/accounts/u8m21ercgelj/users?
I need to extract u8m21ercgelj from url. I've tried it with the code below, but it doesn't work for cases like http://localhost:8024/accounts/u8m21ercgelj
that is, without 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 anyone help me?
source to share
-
[accounts]
not trying to find the wordaccounts
, but one character that is eithera
,c
(a repeat does not change the character),o
,u
,n
,t
ors
because[...]
a character class . So get rid of those[
and]
and replace them with/
as you most likely don't want to accept cases like/specialaccounts/
, but only/accounts/
. -
It looks like you just want to find the next non-section after
/accounts/
. In this case, you can simply use/accounts/([^/]+)
-
If you are sure there will only be one
/accounts/
section in the url (and for more readable code), change yourwhile
toif
or even a conditional statement. It is also not necessarycontains("/accounts/")
, as it simply adds additional movement across the entire line, which can be done infind()
. -
It doesn't look like your method is using the data stored in your class (any fields), so it might 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
source to share