简体   繁体   中英

Removing leading NewLine chars from String by regex?

I am new to "REGEX".And this question is training and educational for regex in 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:

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?

无法修改字符串:replaceAll返回更改后的字符串。

str = str.replaceAll(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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