[英]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: 正则表达式的工作方式如下:
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.