Java regex content between single quotes

26,432

Solution 1

This should do the trick:

(?:^|\s)'([^']*?)'(?:$|\s)

Example: http://www.regex101.com/r/hG5eE1

In Java (ideone):

import java.util.*;
import java.lang.*;
import java.util.regex.*;

class Main {

        static final String[] testcases = new String[] {
            "'Tumblr' is an amazing app",
        "Tumblr is an amazing 'app'",
        "Tumblr is an 'amazing' app",
        "Tumblr is 'awesome' and 'amazing' ",
        "Tumblr's users' are disappointed ",
        "Tumblr's 'acquisition' complete but users' loyalty doubtful"
        };

    public static void main (String[] args) throws java.lang.Exception {
        Pattern p = Pattern.compile("(?:^|\\s)'([^']*?)'(?:$|\\s)", Pattern.MULTILINE);
        for (String arg : testcases) {
            System.out.print("Input: "+arg+" -> Matches: ");
            Matcher m = p.matcher(arg);
            if (m.find()) {
                System.out.print(m.group());
                while (m.find()) System.out.print(", "+m.group());
                System.out.println();
            } else {
                System.out.println("NONE");
            }
        } 
    }
}

Solution 2

If you don't allow the single quote character, ', or the space character, ' ', to be in the pattern, then you're good to go. I used + because I assumed you don't want an empty entry (if not, change it back to an *):

Pattern p = Pattern.compile("'([^' ]+)'");

Solution 3

Try the next:

'\w+'|'\w+(\s\w+)*'

https://github.com/paul-vargas/java-regex-ui

Share:
26,432
user1744332
Author by

user1744332

Updated on March 28, 2020

Comments

  • user1744332
    user1744332 about 4 years

    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:

    Pattern p = Pattern.compile("'([^']*)'");
    
    1. Test Case: 'Tumblr' is an amazing app Expected output: Tumblr

    2. Test Case: Tumblr is an amazing 'app' Expected output: app

    3. Test Case: Tumblr is an 'amazing' app Expected output: amazing

    4. Test Case: Tumblr is 'awesome' and 'amazing' Expected output: awesome, amazing

    5. Test Case: Tumblr's users' are disappointed Expected output: NONE

    6. Test Case: Tumblr's 'acquisition' complete but users' loyalty doubtful Expected output: acquisition

    I appreciate any help with this.

    Thanks.

  • guido
    guido almost 11 years
    This won't match " ... 'amazing and beautiful ' ... "
  • johnchen902
    johnchen902 almost 11 years
    @guido Try the new solution.
  • guido
    guido almost 11 years
    better, but you still have space instead of whitespace, and doesn't get end of line (then it's like mine ;-)
  • guido
    guido almost 11 years
    This works for the input, tough it should be noted that Paul Vargas and my pattern would also accept whitespace inside matched string.
  • Steve P.
    Steve P. almost 11 years
    I agree; your solutions are good and make sense, but none of the example inputs suggested that OP wanted to capture multiple words, so in an effort to keep it as close as possible to OP's code/restrictions, I came up with this.
  • Paul Vargas
    Paul Vargas almost 11 years
    @guido You are right. But the Original Poster was convinced that it is not possible. Sad story.
  • user1744332
    user1744332 almost 11 years
    I realized that your answer is more robust as I tested more scenarios. Wish I have enough reputation to upvote the answer.
  • dermoritz
    dermoritz about 9 years
    it's not really complete because the single quotes are part of the output ;-). It could be fixed with m.group().replace('\'', '\u0000')
  • guido
    guido about 9 years
    @dermoritz no need for that; the same regex will capture the string with quotes in group(0) and without quotes in group(1). ideone.com/m9t1AF
  • sathish anish
    sathish anish over 6 years
    @ᴳᵁᴵᴰᴼ. I have an small problem. So, I want to correct pattern for following strings. "'Tumblr’ is \"an amazing\" app", "Tumblr is an amazing ‘app’", "Tumblr is an “only national citizens can own land.” foreigners’ ", "Tumblr is ‘awesome ‘and’ amazing’ ", "Tumblr’s ‘users’ ‘are’ disappointed’ ss", "Tumblr’s ‘acquisition’ complete but users’ loyalty doubtful", If you know please let me know.