user1744332
user1744332

Reputation: 163

Java regex content between single quotes

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.

Upvotes: 16

Views: 29454

Answers (5)

Paul Vargas
Paul Vargas

Reputation: 42020

Try the next:

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

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

Upvotes: 2

guido
guido

Reputation: 19194

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");
            }
        } 
    }
}

Upvotes: 18

Steve P.
Steve P.

Reputation: 14699

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("'([^' ]+)'");

Upvotes: 7

johnchen902
johnchen902

Reputation: 9601

Just don't let ' ' appear in the output. Use this regex:

'([^' ]*)'

Or make sure the quote pair is wrapped by spaces.

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

Upvotes: 1

Ria
Ria

Reputation: 10347

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
}

Upvotes: 0

Related Questions