[英]Removing leading NewLine chars from String by regex?
I am new to "REGEX".And this question is training and educational for regex in java. 我是“ REGEX”的新手。这个问题是关于Java正则表达式的培训和教育。
I try to remove leading new line chars from a string.(I know we can do this by 'trim()',but I do not want use it).I use '^[\\r\\n]+' as regex,like this: 我尝试从字符串中删除开头的换行符。(我知道我们可以通过'trim()'来做到这一点,但我不想使用它)。我使用'^ [\\ r \\ n] +'作为正则表达式,像这样:
str = "\r\n\r\ntest";
str.replaceAll("^[\r\n]+", "");
System.out.print(str);
I guess result will be 我想结果会是
test
测试
But the result is: 但是结果是:
CRLF
CRLF
test
As you can see,leading new line chars do not removed.Also I try '^\\s' or '^\\s+' as regex,but result was same.Do you know why those regexes do not match leading new line chars? 如您所见,不会删除前导新行字符。我也尝试使用'^ \\ s'或'^ \\ s +'作为正则表达式,但结果相同。您知道为什么这些正则表达式与前导新行字符不匹配吗?
无法修改字符串:replaceAll返回更改后的字符串。
str = str.replaceAll(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.