0

I've a url like

https://example.com/helloworld/@.id==imhere 

or

https://example.com/helloworld/@.id==imnothere?param1=value1

I want to extract the value imhere or imnothere from these URLs.

Pattern.compile("(?<=helloworld\\/@\\.id==).*(?=\\?)"); 

Problem with this one is it does not found ? (first case) it is not matching the pattern.

Can someone help me to fix this? Sorry my mistake, I've missed @.id phase in the URL.

3 Answers3

2

This expression should do it:

^.*@==(.*?)(?:\?.*)?$ 

regex101 demo

It searches for @== and grabs everything after this string, up to a ?, if any. The trick is the lazy *.

The actual match is in group one. Translated to Java, a sample application would look like this:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Sample {
    private static final String PATTERN_TEMPLATE = "^.*@==(.*?)(?:\\?.*)?$";

    public static void main (final String... args) {
        final Pattern pattern = Pattern.compile(PATTERN_TEMPLATE);

        final String firstTest = "https://example.com/helloworld/.@==imhere";
        final Matcher firstMatcher = pattern.matcher(firstTest);
        if (firstMatcher.matches()) {
            System.out.println(firstMatcher.group(1));
        }

        final String secondTest =
                "https://example.com/helloworld/.@==imnothere?param1=value1";
        final Matcher secondMatcher = pattern.matcher(secondTest);
        if (secondMatcher.matches()) {
            System.out.println(secondMatcher.group(1));
        }
    }
}

Ideone demo

If one wants to incorporate the regex to also validate that helloworld/. is present, then one can simply extend the regular expression:

^.*helloworld\/\.@==(.*?)(?:\?.*)?$

regex101 demo

But one should be careful when translating this expression to Java. The backslashes have to be escaped.

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Thank you very much. Since I missed the 'id' phase in the URL, after adding it in your expression it worked for me. I've to use the phrase helloworld, so using the second expression as : "^.*helloworld\\/@\\.id==(.*?)(?:\\?.*)?$" – Nabanita Sen Dec 26 '19 at 22:22
  • You are welcome. Feel free to upvote and or/mark my post as answer. – Turing85 Dec 26 '19 at 22:26
-1

To match after a specific phrase up to the next whitespace, question mark, or end of string, the Java regex is this :

"(?<=helloworld/\\.@==).*?(?![^\\s?])"

https://regex101.com/r/9yraPt/1

If spanning lines, ad (?s) to the beginning.

user229044
  • 232,980
  • 40
  • 330
  • 338
-1

I would not use a regular expression for this. It’s a heavyweight solution for a simple problem.

Use the URI class to extract the path (the part between the host and the ?, if any), then look for the last occurrence of ==:

String s = "https://example.com/helloworld/@.id==imnothere?param1=value1";

URI uri = URI.create(s);
String path = uri.getPath();
int idAttributeIndex = path.lastIndexOf("@.id==");
String id = path.substring(idAttributeIndex + 6);
VGR
  • 40,506
  • 4
  • 48
  • 63