简体   繁体   中英

java does not work for general regex

I am trying to get the following code work:

String kk = "///hello///";
kk = kk.replace("^(\\/*)", "");
System.out.println(kk);

This should take out the '/'s from the beginning.

This regex definitely works: https://regex101.com/

but it does not work in java. could someone please help me. am i missing something?

thanks in advance.

You need to use replaceFirst or replaceAll , since replace won't accept regex as argument.

kk = kk.replaceFirst("^(\\/*)", "");

or

And you don't need to escape forward slashes in java.

kk = kk.replaceFirst("^/+", "");

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