Java 将一组开头有4个空格的线括起来

Java 将一组开头有4个空格的线括起来,java,regex,Java,Regex,我想在 ^[ ]{4}(.*)(?!=^[ ]{4}) 输入: Here is the code: <pre>String name = "Jon";</pre> <pre>System.out.println("Hello "+name);</pre> output: <pre>Hello Jon</pre> 实际产量: Here is the code: <pre>

我想在

^[ ]{4}(.*)(?!=^[ ]{4})

输入:

Here is the code: 
    <pre>String name = "Jon";</pre> 
    <pre>System.out.println("Hello "+name);</pre>
output: 
    <pre>Hello Jon</pre> 
实际产量:

Here is the code:
<pre>
    String name = "Jon";
    System.out.println("Hello "+name);
</pre>
output:
<pre>
    Hello Jon
</pre>
以下是代码:
text.replaceAll(regex, "<pre>$1</pre>");
String name=“Jon”; System.out.println(“Hello”+name); 输出: 你好,乔恩
预期产出:

以下是代码:
text.replaceAll(regex, "<pre>$1</pre>");
String name=“Jon”; System.out.println(“Hello”+name); 输出: 你好,乔恩
Java示例代码:

text.replaceAll(regex,“$1”);
您可以使用:

String out=input.replaceAll((?m)((?:^{4}\\S.*$\\r?\\n)*^{4}\\S.*$)”,
“\\n$1\\n”);

说明:

(?m)#启用多行模式
^{4}\\S.*$#将一行的开头精确匹配为4个空格
\\r?\\n#后跟换行字符
(?:^{4}\\S.*$\\r?\\n)*#匹配0或更多这样的行
^{4}\\S.*$#后跟一行,开头有4个空格
\\n$1\\n#替换为换行符匹配的块换行符

请显示您的代码。^(\s){4}(.*)(?!=^(\s){4})…行吗?忽略换行符:谢谢,让我试着理解它。添加了一个关于regex的解释,当然regex101演示链接也有一些细节。最后一部分
^{4}\\s.$
没有理解。为什么周围没有负面的表情?为什么需要
^{4}\\S.*$
?哦,没有前瞻性。最后一部分是
^{4}\\S.*$
,它匹配最后一行,没有换行符,这样我们就可以避免在
之前添加两行换行符,否则正则表达式可以简化为:
(?m)((?:^{4}\\S.$\\r?\\n?)+
还有。是的,你可以问尽可能多的问题,我会因为一些工作而离线几个小时,但一旦我恢复在线,我会回答所有问题。
String out = input.replaceAll("(?m)((?:^ {4}\\S.*$\\r?\\n)*^ {4}\\S.*$)", 
                              "<pre>\\n$1\\n</pre>");
(?m)                    # enable multilie mode
^ {4}\\S.*$             # match a line with exact 4 spaces at start
\\r?\\n                 # followed by a line feed character
(?:^ {4}\\S.*$\\r?\\n)* # match 0 or more of such lines
^ {4}\\S.*$             # followed by a line with exact 4 spaces at start
<pre>\\n$1\\n</pre>     # replace by <pre> newline matched block newline </pre>