I need to search for the first occurrence of text between single quotes, return the text (without the quotes), but if there is no quoted text is found, return null. I cannot use any additional methods. For example:
- On input "this is some 'text' and some 'additional text'" the method should return: text
- On input "this is an empty string '' and another 'string'" it should return: nothing.
My code:
public static String findSingleQuotedTextSimple(String input) {
Pattern pattern = Pattern.compile ("(?:^|\\s)'([^']*?)'(?:$|\\s)");
Matcher matcher = pattern.matcher (input);
if (matcher.find()) {
return (matcher.group ());
}
else {
return null;
}
}
However I seem to fail the test and the output is null every time, please help me point out the problem.