0

I tried to write a RegEx

Log.e("before: ", number);
number = number.replaceAll("^[0]{0,4}", "+"); // e.g. 004912345678 -> +4912345678
Log.e("after: ", number);

that makes from

004912345678

+4912345678

and in fact it works, BUT it also makes from

+4912345678

++4912345678

and that I don't want. I don't know why it matches strings, that begin with +, since I wrote ^[0]... in my pattern.

1 Answer 1

2

Use ^[0]{1,4} instead of ^[0]{0,4}.

^[0]{0,4} will also accept zero of 0 and as you can see +4912345678 has zero of zeroes so regex will be able to found match and place + there.

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

6 Comments

ah you mean I replace what is before + (actually nothing for humans but in fact "something" for computers) with +?
I just saw you consider yourself a regex fan. can I ask you something? is there a way to talk private for a second?
There is a chat room, but I never used it. Will try to create a room.
mh okay then let me try to do it here very quick, I try to get phone numbers in this format: +(country code)number, if possible for as much countries as possible. I did it like this, first I checked whether the first char is 0 and second is NOT, if true: replace it with +(country code)number, then I checked with the regex you just showed me whether its something like 00(country code)number.... then +(country code)number. do you think that is enough?
Well, that seems to be good subject for separate question. This way you would be able to describe it in more detail and lot of experts could guide you (I am just a fan).
|

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.