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?

+3


source to share


2 answers


  • [accounts]

    not trying to find the word accounts

    , but one character that is either a

    , c

    (a repeat does not change the character), o

    , u

    , n

    , t

    or s

    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 your while

    to if

    or even a conditional statement. It is also not necessary contains("/accounts/")

    , as it simply adds additional movement across the entire line, which can be done in find()

    .

  • 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

      

+3


source


Your regex is written so that it expects to receive a trailing slash - which is what the slash after it means (.*?)

.



You have to change this so that it can accept either trailing slash or end of line. (/|$)

should work in this case, i.e. your regex would be[accounts]/(.*?)(/|$)

+4


source







All Articles