简体   繁体   中英

Java Regular Expressions with String replace

I Need help with Java Regular Expressions?

I have a String "TA520" and "TA011" and I want to get the number without the leading digit using regular expressions. So I need the "520" and the "11" without the leading 0 digits. I have the expression aString = aString.replace("TA0* , ""); but this doesn't work. How would I do this with regular expressions in Java? Thank you.

The problem here is that String.replace does not use regular expressions.

You need to use String.replaceAll .

aString.replaceAll("^TA0*", "")

or alternatively, using replaceFirst :

aString.replaceFirst("^TA0*", "")

This strips away the leading "TA" plus any optional leading zeros.

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