0

I have a string like:

some text here 'quoted text here' some more text

I'd like to get the 'quoted text here' without the quotes. Besides using indexOf(') and then substring, how can I look this up using a regular expression in Java?

Dinko Ivanov
  • 150
  • 2
  • 12
  • 1
    Use this regex `'([^']*)'` and use first captured group – anubhava Nov 28 '17 at 17:09
  • Split it on the single quotes, and get element 1 (and other odd-numbered elements, if appropriate) from the resulting array. – Andy Turner Nov 28 '17 at 17:10
  • Use **capturing groups**. For example with a pattern like `'(.+)'` and then start matching this with `Matcher#find` and extract the group with `Matcher#group`, the group is **group 1**. You may need to escape `'` as `\\'`, I'm not sure, just test it. – Zabuzard Nov 28 '17 at 17:27

1 Answers1

2
String text = "some text here 'quoted text here' some more text";
String regex = "'(.*?)'";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(text);
if (m.find()){
        String s = m.group(1);
        System.out.println(s);
}
magnum87
  • 324
  • 1
  • 11