replace
is a java Method of Java's String, which replace a character with another:
assert "1+555-551-5551".replace('1', ' ') == " +555-55 -555 "
What you are looking for is replaceAll
, which would replace all occurrences of a regex, or replaceFirst
, that would replace the first occurrence only:
assert "1+555-551-5551".replaceAll(/1/, "") == "+555-55-555"
assert "1+555-551-5551".replaceFirst(/1/, "") == "+555-551-5551"
The ^
in your regex means that the one must be at the beginning:
assert "1+555-551-5551".replaceAll(/^1/, "") == "+555-551-5551"
so the code you posted was almost correct.
def mphone = "1+555-555-5555"
(with quotes)