简体   繁体   English

正则表达式,用于删除字符串“ foo bar”之后的所有内容(包括多行)

[英]Regex for deleting everything (including multiple lines) after a string “foo bar”

Need to delete everything after two words that are found together. 需要删除一起找到的两个单词之后的所有内容。 Tried looking around for similar questions 试图寻找类似的问题

str.replaceAll("foo bar(.*)", "")

Only seems to delete the first line. 似乎只删除第一行。

When i add $ to the end of the regex ( foo bar.*$ ) it doesn't seem to pick anything at all? 当我在正则表达式(foo bar。* $)的末尾添加$时,似乎根本没有选择任何东西? Any ideas why? 有什么想法吗?

Thanks 谢谢

Use Single Line Mode matching using flag ?s . 通过标志?s使用Single Line Mode匹配。

str = str.replaceAll("(?s)foo bar(.*)", "");

If using regex is not mandatory, you can simply use the following: 如果不是必须使用正则表达式,则可以简单地使用以下命令:

str = str.indexOf("foo bar") == -1 ? str : str.substring(0, str.indexOf("foo bar"));

If it's as simple as finding 2 words, you could use indexOf to find them, then use substring to trim the String to drop everything after the two words. 如果只是查找两个单词一样简单,则可以使用indexOf查找它们,然后使用substring修整String以便将所有单词都放在两个单词之后。

String keyWord = "key word";
String input = "some\ntext\nkey word is here\nand more text\nto follow";
int pos = input.indexOf(keyWord);
if (pos != -1) {
    String trimmed = input.substring(0, pos + keyWord.length());
    System.err.println("'" + trimmed + "'");
}

The code above assumes you want to keep the key word; 上面的代码假定您要保留关键字; if you don't want to keep it, just drop the + keyword.length() in the substring . 如果您不想保留它,只需在substring删除+ keyword.length()

If you do want to use a regular expression, pass the Pattern.DOTALL flag to have the dot match line endings. 如果确实要使用正则表达式,请传递Pattern.DOTALL标志以使点匹配线的结尾。 You also need to change the regex to capture what you want to keep, not what you want to discard (as your example has it): 您还需要更改正则表达式以捕获要保留的内容,而不是要舍弃的内容(如您的示例所示):

String keyWordRegex = "key\\s+word";
Pattern pattern = Pattern.compile("(.*\\b" + keyWordRegex + "\\b).*", Pattern.DOTALL);
Matcher matcher = pattern.matcher(input);
String replaced = matcher.replaceAll("$1");
System.err.println("'" + replaced + "'");

The regex works like this: 正则表达式的工作方式如下:

  • first, opening brace to start capturing 首先,打开支架开始捕捉
  • capture anything (dot) zero to infinite times (star) 捕获零到无限次(星)的任何内容(点)
  • then a word boundary, so that it does only match "key word" but not "masterkey word" 然后是一个单词边界,因此它只匹配“关键字”,而不匹配“万能关键字”
  • then your key word regex, in this example I made it to be "key", one or more (plus) spaces (\\s), and "word" 然后是您的关键字正则表达式,在此示例中,我将其设为“关键字”,一个或多个(加号)空格(\\ s)和“单词”
  • word boundary again 再次字边界
  • close the capture group 关闭捕获组
  • allow any number (star) of characters (dot) 允许任意数量(星号)的字符(点)

you can try below solution. 您可以尝试以下解决方案。 It's delete all character after the foo bar is found. 找到foo bar后删除所有字符。

String a = "hell fdshf fdfjk foo bar ffsd fsajkh vsfdsv" ;
String as = a.split("foo bar")[0];
System.out.println(as);

Output 输出量

hell fdshf fdfjk

There is no need to use regex. 无需使用正则表达式。 You can use simple string manipulation concept. 您可以使用简单的字符串操作概念。

String str = "testing test foo bar  test  foo bar test \n testing all  \n foo bar(.*) tester";

        String boundaryValueText = "foo bar";
        int boundaryValueIndex = str.indexOf(boundaryValueText);
        String parsedContent = boundaryValueIndex != -1 ? new StringBuilder(str)
                .delete(boundaryValueIndex + boundaryValueText.length(), str.length()).toString() : str; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM