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:
Tim Cooke
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Liutauras Vilda
Sheriffs:
Rob Spoor
Junilu Lacar
paul wheaton
Saloon Keepers:
Stephan van Hulst
Tim Moores
Tim Holloway
Carey Brown
Scott Selikoff
Bartenders:
Piet Souris
Jj Roberts
fred rosenberger
Forum:
Programmer Certification (OCPJP)
can some one explain me the output of this program?
Pooja Oza
Greenhorn
Posts: 21
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Given: import java.util.regex.*; class Regex2 { public static void main(String[] args) { Pattern p = Pattern.compile(args[0]); Matcher m = p.matcher(args[1]); boolean b = false; while(b = m.find()) { System.out.print(m.start() + m.group()); } } } And the command line: java Regex2 "\d*" ab34ef What is the result? A. 234 B. 334 C. 2334 D. 0123456 E. 01234456 F. 12334567 G. Compilation fails Answer: ® 3 E is correct. The \d is looking for digits. The * is a quantifier that looks for 0 to many occurrences of the pattern that precedes it. Because we specified *, the group() method returns empty Strings until consecutive digits are found, so the only time group() returns a value is when it returns 34 when the matcher finds digits starting in position 2. The start() method returns the starting position of the previous match because, again, we said find 0 to many occurrences.
Thanks,
Pooja Oza
Ulf Dittmer
Rancher
Posts: 43028
76
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That's a FAQ:
http://faq.javaranch.com/java/ScjpFaq#kb-regexp
In the future, please
QuoteYourSources
when posting questions like these.
Pooja Oza
Greenhorn
Posts: 21
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks Ulf for your help
Thanks,
Pooja Oza
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Doubt in K&B Book.
K & B book- chapter 6 - excercie1-not able to understand
Regex Doubt
K&B Study Guide for Java 5 p498 Selftest problem 1
SCJP book regex, pattern and matcher question
More...