Java regex content between single quotes
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+)*'
user1744332
Updated on March 28, 2020Comments
-
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("'([^']*)'");
Test Case: 'Tumblr' is an amazing app Expected output: Tumblr
Test Case: Tumblr is an amazing 'app' Expected output: app
Test Case: Tumblr is an 'amazing' app Expected output: amazing
Test Case: Tumblr is 'awesome' and 'amazing' Expected output: awesome, amazing
Test Case: Tumblr's users' are disappointed Expected output: NONE
Test Case: Tumblr's 'acquisition' complete but users' loyalty doubtful Expected output: acquisition
I appreciate any help with this.
Thanks.
-
guido almost 11 yearsThis won't match " ... 'amazing and beautiful ' ... "
-
johnchen902 almost 11 years@guido Try the new solution.
-
guido almost 11 yearsbetter, but you still have space instead of whitespace, and doesn't get end of line (then it's like mine ;-)
-
guido almost 11 yearsThis works for the input, tough it should be noted that Paul Vargas and my pattern would also accept whitespace inside matched string.
-
Steve P. almost 11 yearsI 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 almost 11 years@guido You are right. But the Original Poster was convinced that it is not possible. Sad story.
-
user1744332 almost 11 yearsI realized that your answer is more robust as I tested more scenarios. Wish I have enough reputation to upvote the answer.
-
dermoritz about 9 yearsit's not really complete because the single quotes are part of the output ;-). It could be fixed with m.group().replace('\'', '\u0000')
-
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 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.