Reputation: 69
I want to replace an entire String with ""
(an empty string).
I have a string, like the one that follows:
<code code="34068-7"
However, the number at the end can be anything (in this example it is 7),
So, basically, I want to replace everything between <
and the final number, including the <
and the final number, with ""
. Since the number can be any digit, how can I do this with regex?
I have tried this:
line = line.replaceAll("<code.*?\">","");
Can anyone suggest a way to achieve this?
Upvotes: 2
Views: 63
Reputation: 54
Adding to ernest_k's answer, you can also replace numbers in regex with \d for better readability. So the regex would be:
<code code=\"[\d-]+\"
Upvotes: 0
Reputation: 567
Try this:
line = line.replaceAll("(?<=\\<)code.*?\"", "");
This lookbehind regexp will ensure that the <
at the beginning of your string will not be replaced by "", so your example output will be
<34068-7"
Upvotes: 0
Reputation: 45319
You can use this pattern:
line = line.replaceAll("<code code=\"[0-9-]+\"", "");
That pattern is only flexible about the data in the attribute value, the rest is assumed static.
Upvotes: 2