Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
Regular Expression
Nilesh Garje
Greenhorn
Posts: 15
1
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Why program print Invalid Mobile Number?
package com.nit.regex; import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexDemoMobileNo { public static void main(String[] args) { Pattern pattern=Pattern.compile("(0|91)?[7-9][0-9]{9}"); Matcher matcher=pattern.matcher(args[0]); System.out.println(matcher.find()); if(matcher.find()) { System.out.println("Valid Mobile Number"); } else { System.out.println("InValid Mobile Number"); } } } /* //Output: E:\scjpprogram\OCP>javac -d . RegexDemoMobileNo.java E:\scjpprogram\OCP>java com.nit.regex.RegexDemoMobileNo 9595542326 true InValid Mobile Number E:\scjpprogram\OCP> */
Campbell Ritchie
Marshal
Posts: 79151
377
posted 6 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Because you are calling find() twice; you only find the number once, then on the second attempt it will return false.
Campbell Ritchie
Marshal
Posts: 79151
377
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Don't use com.nit in your package names because somebody else owns that web address.
Nilesh Garje
Greenhorn
Posts: 15
1
posted 6 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Ritchie
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Usage of Regular Expression
How to find if there is a number in a string using regular expressions.
Counting exact matches of substring.
pattern matches
string processing
More...