0

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.

1
  • 2
    "Doesn't work" is not a problem description. Commented Dec 20, 2012 at 1:00

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.