简体   繁体   中英

Java regex to remove all trailing numbers?

I want to remove any numbers from the end of a string, for example:

"TestUser12324" -> "TestUser"
"User2Allow555" -> "User2Allow"
"AnotherUser" -> "AnotherUser"
"Test123" -> "Test"

etc.

Anyone know how to do this with a regular expression in Java?

This should work for the Java String class, where myString contains the username:

myString = myString.replaceAll("\\d*$", "");

This should match any number of trailing digit characters (0-9) that come at the end of the string and replace them with an empty string.

.

假设值在字符串中,s:

    s = s.replaceAll("[0-9]*$", "");

这应该是正确的表达方式:

(.+[^0-9])\d*$

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