简体   繁体   English

带字符串替换的Java正则表达式

[英]Java Regular Expressions with String replace

I Need help with Java Regular Expressions? 我需要Java正则表达式的帮助吗?

I have a String "TA520" and "TA011" and I want to get the number without the leading digit using regular expressions. 我有一个字符串"TA520""TA011" ,我想使用正则表达式获取不带前导数字的数字。 So I need the "520" and the "11" without the leading 0 digits. 因此,我需要不带前0位的"520""11" 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. 我有表达式aString = aString.replace("TA0* , "");但这不起作用,我该如何用Java中的正则表达式来做到这一点?

The problem here is that String.replace does not use regular expressions. 这里的问题是String.replace不使用正则表达式。

You need to use String.replaceAll . 您需要使用String.replaceAll

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

or alternatively, using replaceFirst : 或者,使用replaceFirst

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

This strips away the leading "TA" plus any optional leading zeros. 这样会去除前导“ TA”以及任何可选的前导零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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