Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式,删除每行的前导空格_Java_Regex - Fatal编程技术网

Java正则表达式,删除每行的前导空格

Java正则表达式,删除每行的前导空格,java,regex,Java,Regex,要使用Java String.replaceAll(regex,“”)删除多行文本字符串中每行的所有前导空格。如有可能,还应移除所有回车。“regex”应该是什么?将我的评论转换为答案,以便为未来的访问者找到解决方案 您可以在Java中使用正则表达式替换: str = str.replaceAll("(?m)^\\s+|\\s+$", ""); 正则表达式详细信息: (?m):启用多行模式,以便在每行中匹配^和$ ^\\s+:在行开始处匹配1+个空格 |:或 \\s+$:在行尾前匹配1+个空

要使用Java String.replaceAll(regex,“”)删除多行文本字符串中每行的所有前导空格。如有可能,还应移除所有回车。“regex”应该是什么?

将我的评论转换为答案,以便为未来的访问者找到解决方案

您可以在Java中使用正则表达式替换:

str = str.replaceAll("(?m)^\\s+|\\s+$", "");
正则表达式详细信息:

  • (?m)
    :启用
    多行
    模式,以便在每行中匹配
    ^
    $
  • ^\\s+
    :在行开始处匹配1+个空格
  • |
    :或
  • \\s+$
    :在行尾前匹配1+个空格