在单引号之间的Java regex内容

时间:2022-09-15 16:13:58

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:

我正在尝试用Java编写一个regex来查找单引号之间的内容。能帮我个忙吗?我试过以下方法,但在某些情况下行不通:

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

    测试案例:“Tumblr”是一款令人惊叹的应用程序,预计会输出:Tumblr

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

    测试案例:Tumblr是一个令人惊叹的“应用”预期输出:应用

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

    测试案例:Tumblr是一款“令人惊叹”的应用

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

    测试用例:Tumblr“棒极了”和“惊人”的预期输出:棒极了,棒极了。

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

    测试案例:Tumblr的用户对预期的结果感到失望:没有

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

    测试案例:Tumblr的“收购”完成,但用户的忠诚度令人怀疑的预期产出:收购

I appreciate any help with this.

我很感激你的帮助。

Thanks.

谢谢。

5 个解决方案

#1


14  

This should do the trick:

这应该能达到目的:

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

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

例如:http://www.regex101.com/r/hG5eE1

In Java (ideone):

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

#2


5  

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

#3


1  

Try the next:

试着下一个:

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

在单引号之间的Java regex内容

#4


0  

Try this simple regex pattern:

尝试这个简单的regex模式:

'([^\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
}

#5


0  

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

不要让' '出现在输出中。使用这个正则表达式:

'([^' ]*)'

Or make sure the quote pair is wrapped by spaces.

或者确保报价对被空格包围。

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

#1


14  

This should do the trick:

这应该能达到目的:

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

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

例如:http://www.regex101.com/r/hG5eE1

In Java (ideone):

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

#2


5  

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

#3


1  

Try the next:

试着下一个:

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

在单引号之间的Java regex内容

#4


0  

Try this simple regex pattern:

尝试这个简单的regex模式:

'([^\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
}

#5


0  

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

不要让' '出现在输出中。使用这个正则表达式:

'([^' ]*)'

Or make sure the quote pair is wrapped by spaces.

或者确保报价对被空格包围。

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