简体   繁体   English

查找和替换基于正则表达式Java的整行

[英]Find and replace whole line based on regex java

I have this string 我有这串

Chest pain\tab \tab 72%\tab 0%\tab 67%
 }d \ql \li0\ri0\nowidctlpar\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\tx9360\tx10080\tx10800\tx11520\tx12240\tx12960\faauto\rin0\lin0\itap0 {\insrsid14762702 
 }d \ql \li0\ri0\nowidctlpar\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\tx9360\tx10080\faauto\rin0\lin0\itap0 {\b\f1\fs24\ul\insrsid14762702 Waveform}{\insrsid14762702 
 }{\insrsid14762702 {\*\shppict{\pict{\*\picprop\shplid1025{\sp{\sn shapeType}{\sv 75}}{\sp{\sn fFlipH}{\sv 0}}{\sp{\sn fFlipV}{\sv 0}}{\sp{\sn fLine}{\sv 0}}{\sp{\sn fLayoutInCell}{\sv 1}}}
\

I want to get rid of all lines with }d \\ql in them 我想摆脱所有带有}d \\ql的行

I have tried 我努力了

String v= u.replace("}d \\ql(\\.*)","");

but it doesn't detect the line. 但它无法检测到线路。 Having tested it out the culprit must be the .* part but I don't know how to put it in the string.replace 经过测试的罪魁祸首必须是。*部分,但我不知道如何将其放入字符串中。

replace doesn't use regex syntax, replaceAll does. replace不使用正则表达式语法, replaceAll使用。 This means that \\\\.* simply replace text which represents \\ . 这意味着\\\\.*只需替换代表\\文本. and * . *

So your first solution could look like (notice that to create \\ literal in regex you need to escaped it twice: once in regex \\\\ and once in string literal "\\\\\\\\" ) 因此,您的第一个解决方案可能看起来像这样(注意,要在正则表达式中创建\\文字,您需要对其进行两次转义:一次在regex \\\\ ,一次在字符串文字"\\\\\\\\"

String v = u.replaceAll("\\}d \\\\ql.*","");

But possible problem here is that we don't require \\} to be placed at start of string. 但是这里可能的问题是我们不要求将\\}放在字符串的开头。 Also we are skipping leading space in that line which exist right before \\} . 同样,我们也跳过了\\}之前存在的行首空间。
To solve it we can add ^\\s* at start of your regex and make ^ represent start of line (we can do it with MULTILINE flag - we can use (?m) for that). 为了解决这个问题,我们可以在正则表达式的开头添加^\\s* ,并使^代表行的开头(我们可以使用MULTILINE标志来实现-我们可以使用(?m) )。

So now our solution could look like: 所以现在我们的解决方案可能看起来像:

String v= u.replaceAll("(?m)^\\s*\\}d \\\\ql.*","");

But there is another problem. 但是还有另一个问题。 . can't match line separators so .* will not include them in match which will prevent us from removing them. 无法匹配行分隔符,因此.*不会将它们包括在匹配项中,这将阻止我们删除它们。
So we should include them in our match explicitly (we should also make them optional - we can use ? quantifier for that - in case line you want to match will be last one, which means it will not have line separator after it). 因此,我们应该将它们明确包含在我们的匹配中(我们还应该使它们成为可选-我们可以使用?量词-如果要匹配的行是最后一行,这意味着它后面将没有行分隔符)。 Since Java 8 we can do it with \\R which can match few line separators (including paragraph separators), or if you want to limit yourself only to \\r \\n (or can't use Java 8) you can use something like (\\r?\\n|\\r) . 从Java 8开始,我们可以使用\\R ,它可以匹配几个行分隔符(包括段落分隔符),或者如果您想将自己限制为\\r \\n (或者不能使用Java 8),则可以使用类似(\\r?\\n|\\r)

So our final solution can look like: 因此,我们的最终解决方案如下所示:

in Java 8 在Java 8中

String v = u.replaceAll("(?m)^\\s*\\}d \\\\ql.*\\R?","");

pre Java 8 Java 8之前的版本

String v = u.replaceAll("(?m)^\\s*\\}d \\\\ql.*(\r?\n|\r)?","");

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

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