【问题标题】:Java Regex - Get all words before substring in StringJava Regex - 获取字符串中子字符串之前的所有单词
【发布时间】:2012-05-12 08:08:00
【问题描述】:

我有一个包含一个句子的字符串,我想根据一个单词将它分成两半。 我有正则表达式(\\w+) word,我认为它可以让我得到“word”+“word”本身之前的所有单词,然后我可以删除最后四个字符。

但这似乎不起作用..任何想法我做错了什么?

谢谢。

【问题讨论】:

  • 代码比描述问题更有帮助。
  • 也许考虑一个非贪婪的限定符 '+?'而不是“+”
  • “这似乎不起作用,”嗯?发生什么了?你想发生什么?
  • 为什么不直接用word呢?使用 Pattern.find 你可以在字符串中找到它的索引

标签: java regex string substring


【解决方案1】:

试试这个:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("^.*?(?= word)");
        Matcher m = p.matcher("Everything before the word");
        while (m.find()) {
            System.out.println(m.group());
        }
    }
}

分解如下:

.*?一切

(?= 之前

单词

) 结束

【讨论】:

  • 哦,是的,愚蠢的尝试很好地总结了它:)我在这里没有粗鲁,我说的是一个事实......
  • 我不明白代码格式是多么必要,因为问题是关于正则表达式本身,我假设他已经知道如何编译表达式。我给出了表达式并将其分解以显示每个部分在做什么。将来我会尝试更具描述性,对堆栈溢出是全新的。
  • 你的编辑已经好多了,我已经清除了反对票。玩得开心!
【解决方案2】:

使用字符串操作:

int idx = sentence.indexOf(word);
if (idx < 0)
  throw new IllegalArgumentException("Word not found.");
String before = sentence.substring(0, idx);

使用正则表达式:

Pattern p = Pattern.compile(Pattern.quote(word));
Matcher m = p.matcher(sentence);
if (!m.find())
  throw new IllegalArgumentException("Word not found.");
String before = sentence.substring(0, m.start());

或者:

Pattern p = Pattern.compile("(.*?)" + Pattern.quote(word) + ".*");
Matcher m = p.matcher(sentence);
if (!m.matches())
  throw new IllegalArgumentException("Word not found.");
String before = m.group(1);

【讨论】:

    【解决方案3】:

    原因是+ 是一个贪婪的量词,它将匹配整个字符串包括您指定的单词,而不返回。

    如果您将其更改为 (\\w+?) word 它应该可以工作(不情愿的量词)。更多关于量词及其确切功能here。

    【讨论】:

    • + 是贪婪的,但它确实允许回溯。所有格等价物是++
    • 好吧,那时我还没有真正想出量词。我认为回溯意味着您实际上指定了正则表达式中的位置和内容?鉴于输入字符串包含他正在寻找的word,relucant 会自动找到 2 个匹配项...
    • 回溯是指表达式“\\w+\\w”将匹配“xy”。匹配器将匹配 "\\w+" 和 "xy",然后意识到没有什么可以匹配第二个 "\\w" 了。因此它将回溯,将“\\w+”与“x”匹配,将第二个“\\w”与“y”匹配。
    【解决方案4】:

    这似乎有效:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Test {
        public static void main(String[] args) {
            Pattern p = Pattern.compile("([\\w\\s]+) word");
            Matcher m = p.matcher("Could you test a phrase with some word");
            while (m.find()) {
                System.err.println(m.group(1));
                System.err.println(m.group());
            }
        }
    }
    

    【讨论】:

      【解决方案5】:

      您需要在单词前后标记句子的每个部分。

      http://docs.oracle.com/javase/1.5.0/docs/api/

       String[] result = "this is a test".split("\\s"); //replace \\s with your word
       for (int x=0; x<result.length; x++)
           System.out.println(result[x]);
      

      【讨论】:

      • 如果需要,我可以帮助扩展我的示例,但快速浏览一下句子的各个部分存储在一个数组中,并且它被您分解句子的单词所分割。
      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 2022-11-10
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多