0
Help in java regex.
Here are some phone numbers. I want to print all phone number without prefix and 0. Note :- phone number should be 10 digit long without (+91 and space and 0) String a = "+918092123456 " + "+91 9431123456" + "9075123456" + "08409123456"; // My code for this.. a = a.replaceAll("\\+91", ""); Pattern p = Pattern.compile("(0)?(\\d{10})"); /* I putted (0)? Because , if i don't put this, the last digit of the phone number which starts with 0 won't print. */ Matcher m = p.matcher(a); While(m.find()){ System.out.println(m.group()); } All prints fine , but in the last phone number. 0 also getting printed.
5 Respostas
+ 1
@LeoBeliik
aah. thanks. now it works fine..
but , i also achieved it. In a very long way..
first converted string into array
took a number first then converted in char then take out its 0th index compared it then against the 0th index..
pheww. that was a lot of hard work...
take a look at the code.. you'll be like OMG!!
public static void sortMobileNo(String array[]) {
for (int i = 0; i < array.length; i++) {
String str = array[i];
char ch[] = str.toCharArray();
if (ch[0] == '0') {
str = str.replaceFirst("0", "");
array[i] = str;
} else if (ch[0] == '+') {
str = str.replaceAll("\\+91", "");
array[i] = str;
}
}
for (int i = 0; i < array.length; i++) {
String str = array[i];
Pattern p = Pattern.compile("(\\d){10}");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group());
}
}
PLEASE SAY SOME WORDS FOR MY CODE. hehehe
+ 1
LeoBeliik yeah..this is also nice
I don't know many predefined method.
I guess . I always write long codes for do something.. i never searched for any extra method on google....
I just know that methods. Which a tutorial has on that java course.
Thanks, for your help.
I really appreciate it.
+ 1
LeoBeliik
When you said you don't need to do regex.. i was like , i am forgetting something..
now i remember why i used regex... Becuase here i putted email, name and phone in a single array... That's why i used regex...
Hehe
0
LeoBeliik yeah...
Thanks for the advice 😊