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.