I am looking for regex with capture group where question mark (?) can be present in my input string. If it is not present it returns the input string as it is, but if ? is present the return the string before the first occurrence of ?.
My input can be in following format
Pattern 1
abc.txt // result should be abc.txt
Pattern 2
abc.txt?param1=qwerty.controller¶m2=xsd.txt // result should be abc.txt
I tried below
Matcher matcher = Pattern.compile("(.*?)\\?").matcher(str1);
String group1 = "";
if (matcher.find()) {
group1 = matcher.group();
}
With this I am able to capture expected result for pattern 2, but I am not sure how to modify it so that I can capture expected result for both pattern 1 and pattern 2.
Update:- I know if group1 is empty string, i can make out that input string does not contain any ? and input string is the expected output here. But i am looking for if i can capture both patterns with single regex ?