I am trying to write a regex in Java to find the content between single quotes. Can one please help me with this? I tried the following but it doesn\'t work in some cases:>
Try this simple regex pattern:
'([^\s']+)'
and a test code:
try {
Pattern regex = Pattern.compile("'([^\\s']+)'");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
for (int i = 1; i <= regexMatcher.groupCount(); i++) {
// matched text: regexMatcher.group(i)
// match start: regexMatcher.start(i)
// match end: regexMatcher.end(i)
}
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}