使用正则表达式从Java中的引用中提取两个字符串?

使用正则表达式从Java中的引用中提取两个字符串?,java,regex,extract,Java,Regex,Extract,我不熟悉使用模式,在互联网上到处寻找这个问题的解释 假设我有一个字符串:string info=“我需要提取的数据是‘这里’和‘也在这里’ 我将如何提取这些词: here also here 没有使用模式的单引号 这就是我目前所拥有的 Pattern p = Pattern.compile("(?<=\').*(?=\')"); Pattern p=Pattern.compile((?我想你把它弄得太复杂了,试试看 Pattern.compile("'([^']+)'"); 或 它们

我不熟悉使用模式,在互联网上到处寻找这个问题的解释

假设我有一个字符串:
string info=“我需要提取的数据是‘这里’和‘也在这里’

我将如何提取这些词:

here
also here
没有使用模式的单引号

这就是我目前所拥有的

Pattern p = Pattern.compile("(?<=\').*(?=\')");

Pattern p=Pattern.compile((?我想你把它弄得太复杂了,试试看

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


它们都可以工作。然后,在执行
matcher.find()

之后,您可以从第一组
matcher.group(1)
中提取结果。我认为您使问题变得复杂,请尝试

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


它们都可以工作。然后,在执行
matcher.find()

后,您可以从第一组
matcher.group(1)
中提取结果,尝试使您的正则表达式不贪婪:

Pattern p = Pattern.compile("(?<=')(.*?)(?=')");
这是因为前向/后向不使用

要解决此问题,请使用正则表达式:

Pattern p = Pattern.compile("'(.*?)'");
甚至更好(更快):


尝试使您的正则表达式不贪婪:

Pattern p = Pattern.compile("(?<=')(.*?)(?=')");
这是因为前向/后向不使用

要解决此问题,请使用正则表达式:

Pattern p = Pattern.compile("'(.*?)'");
甚至更好(更快):


这应该适合您:

    Pattern p = Pattern.compile("'([\\w\\s]+)'");
    String info = "Data I need to extract is 'here' and 'also here'";
    Matcher m = p.matcher(info);
    while (m.find()) {
        System.out.println(m.group(1));
    }
这是打印件:-

here
also here
如果希望将数据分为两个单独的组,可以执行以下操作:-

    Pattern p = Pattern.compile("^[\\w\\s]*?'([\\w\\s]+)'[\\w\\s]*?'([\\w\\s]+)'$");
    String info = "Data I need to extract is 'here' and 'also here'";
    Matcher m = p.matcher(info);
    while (m.find()) {
        System.out.println("Group 1: " + m.group(1));
        System.out.println("Group 2: " + m.group(2));
    }
这是打印件:

Group 1: here
Group 2: also here

这应该适合您:

    Pattern p = Pattern.compile("'([\\w\\s]+)'");
    String info = "Data I need to extract is 'here' and 'also here'";
    Matcher m = p.matcher(info);
    while (m.find()) {
        System.out.println(m.group(1));
    }
这是打印件:-

here
also here
如果希望将数据分为两个单独的组,可以执行以下操作:-

    Pattern p = Pattern.compile("^[\\w\\s]*?'([\\w\\s]+)'[\\w\\s]*?'([\\w\\s]+)'$");
    String info = "Data I need to extract is 'here' and 'also here'";
    Matcher m = p.matcher(info);
    while (m.find()) {
        System.out.println("Group 1: " + m.group(1));
        System.out.println("Group 2: " + m.group(2));
    }
这是打印件:

Group 1: here
Group 2: also here

为什么不简单地使用以下内容

'.*?'

为什么不简单地使用以下内容

'.*?'


此方法是否会返回matcher中的两个组,我可以通过matcher.group(1)和matcher.group(2)访问它们?@user656710:您的原始正则表达式没有任何组。我已将
*?
放在
()
中,因此这两个词都将在组(1)中找到。太好了。非常感谢您的回复。作为旁注,这将如何实现,以便将单词存储在两个单独的组中?此方法是否会在matcher中返回两个组,我可以通过matcher.group(1)和matcher.group(2)访问它们?@user656710:您原来的正则表达式没有任何组。我已将
*?
放入
()
,因此这两个单词都可以在组(1)中找到。太好了。非常感谢您的回复。作为旁注,这将如何实现,以便将单词存储在两个单独的组中?这看起来很好…我如何能够将两段数据拆分为单独的组?我不知道为什么,但似乎该模式没有正确编译…我没有得到任何发现/结果:(我很确定这是可行的,因为我在IDE中运行了这段代码。这看起来不错……我如何才能将这两段数据分成不同的组?我不知道为什么,但似乎模式没有正确编译……我没有得到任何发现/结果:(我非常确定这是可行的,因为我在我的IDE中运行了该代码。有没有办法将结果分为matcher.group(1)和matcher.group(2)?@user656710,是的,使用
([^']+).*([^']+)。
。非常有效!非常感谢!有没有办法将结果分为matcher.group(1)和matcher.group(2)呢)“?@user656710,是的,使用
”([^']+)“.*?”([^']+)”
。效果非常好!非常感谢!