简体   繁体   English

删除除了匹配正则表达式java的所有文本

[英]deleting all text except what matches regex java

I have regexp, that should delete comments(ye, it's bad, but for me it works) 我有regexp,应该删除评论(你们,这很糟糕,但对我来说它有效)

String regex = (^\/\/.*)|(\s+\/\/.*)|((\/\*)(.|\n)+?(\*\/));

this code should delete all commtents 此代码应删除所有密码

Pattern.compile(regex).matcher(some text).replaceAll(" ");

but how can i make it to delete all EXCEPT regexp? 但我如何才能删除所有EXCEPT regexp?

For example: if i have string like this: 例如:如果我有这样的字符串:

/*Comments it's cool*/ public static void foo(){}

my regexp match comments, but i need to delete what doesn't match, so it will look like: 我的正则表达式匹配评论,但我需要删除不匹配的内容,所以它看起来像:

/*Comments it's cool*/

Basically, you can just match what your regex matches, and concatenate the matched texts. 基本上,您可以匹配正则表达式匹配的内容,并连接匹配的文本。 It is much easier. 这更容易。

As for the regex that removes all but what your original pattern matches, you can use consider capturing your whole pattern with a capturing optional group and use a . 至于删除除原始模式匹配之外的所有正则表达式的正则表达式,您可以考虑使用捕获可选组捕获整个模式并使用. to match any char after it. 匹配后的任何字符。

Say, your pattern is abc . 说,你的模式是abc It matches abc . 它匹配abc But now, you need to match the opposite, everything that your regex does not match. 但现在,你需要匹配相反的,你的正则表达式不匹配的一切。 Use 采用

(?s)(abc)?.

And replace with $1 . 并以$1替换。 See the regex demo . 请参阅正则表达式演示

Some adjustment may be necessary though for newlines. 对于换行,可能需要进行一些调整。 If you do not want to remove newlines, do not use the DOTALL modifier. 如果您不想删除换行符,请不要使用DOTALL修饰符。 If you want to use it, replace . 如果要使用它,请替换. with [^\\n\\r] in the original pattern. 在原始模式中使用[^\\n\\r] Remember, you will be able to shrink the empty lines with some (\\r?\\n|\\r){2,} pattern (and again, $1 backreference). 请记住,您可以使用一些(\\r?\\n|\\r){2,}模式(以及$1反向引用)缩小空行。

So, your regex is 所以,你的正则表达式是

String regex = "^\\s*//.*|/\*[^*]*\*+(?:[^/*][^*]*\*+)*/";

where /\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/ matches multiline comments, and ^\\\\s*//.* matches singleline comments. 其中/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/匹配多行注释, ^\\\\s*//.* //。*匹配单行注释。

To make it remove all but what it matches, use 要删除除匹配之外的所有内容,请使用

String regex = "(?sm)(^\\s*//[^\r\n]*|/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/)?.";

and replace with $1 backreference. 并用$1反向引用替换。 See the regex demo (based on ClasG demo ). 查看正则表达式演示 (基于ClasG演示 )。 Note I also added a (?m) MULTILINE modifier for the ^ to match at the beginning of the line. 注意我还为^添加了一个(?m) MULTILINE修饰符,以便在行的开头匹配。

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

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