File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex" Watch "Regex" New topic
Author

Regex

podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
public class Practice {
public static void main(String... args){
Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("a1");
boolean b = false;
while(b = m.find())
System.out.println("Hello");
}
}

It prints,

Hello
Hello
Hello

WHY ??

and another question ...

%b = boolean

%c = ?

%d = digit

%f = float ?

%s = String


* please dont tell me that is because "*" or "?" is greedy because i don't understand what "greedy" means.

* don't tell me go read the javadoc (don't be mean )

Thanks a lot !
[ June 26, 2008: Message edited by: podonga poron ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41151
    
  45
For starters, you might print out something more useful than "Hello", e.g.



Also, you could use a different regex, namely "\\d+". I suspect that's what you're actually looking for. Are you familiar with the exact meaning of "*", "?" and "+" in regexps?

please dont tell me that is because "*" or "?" is greedy because i don't understand what "greedy" means.

Then you had better take steps to learn what it means, hadn't you? It's not the answer here, though.

don't tell me go read the javadoc (don't be mean)

With Java, the answer very often is in the javadocs, so getting into the habit of consulting them is a very useful trait. In particular, the page about the Pattern class is very extensive, telling you just about everything you need to know about how to use regexps in Java. If it contains concepts you're unfamiliar with, ask about them. Not reading the javadocs is not a feasible approach to Java programming.


Ping & DNS - my free Android networking tools app
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Firstly, the javadocs are important.

Rather than printing 'Hello' each time, why not use group() to print out what was matched in each case?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Um, what Ulf said.
podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
Originally posted by David O'Meara:
Firstly, the javadocs are important.

Rather than printing 'Hello' each time, why not use group() to print out what was matched in each case?


because i want to understand WHY "hello" is printed 3 times ! and why if i don't use "*" is printed only one time ..

this is NOT A PRACTICAL QUESTION of course, this is just for the SCJP exam, that's why i didn't use the group() method
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
The key lies in the deep details of what that * means in the regex context. What does it mean? What does * say to return on?


------------------------
Bob
SCJP - 86% - June 11, 2009
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

podonga poron, if you're studying for the SCJP, I assume that means you're preparing for a career in Java programming. A really important part of being a good programmer is learning. There are always new APIs, libraries, tools, and languages to learn. You have to enjoy learning those things, or you're going to be very unhappy with your career choice. My advice to you is to listen to these smart guys; they're not just trying to help you with the answer, they're trying to help you learn how to find the answers on your own. That's an incredibly valuable skill and one you'll absolutely need as your career begins. Take the time to do the background reading, explore all the resources available to you, and learn. You will be glad you did.


[Jess in Action][AskingGoodQuestions]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by podonga poron:
...
this is NOT A PRACTICAL QUESTION of course,...


Lastly, it's very rude to shout at people who are taking time from their busy schedules to help you for free.
Please don't do that.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Ernest Friedman-Hill:
That's an incredibly valuable skill and one you'll absolutely need as your career begins.


Absolutely, and by being able to see the value returned in each case you will (hopefully) be given the tools to answer the question yourself. You could even post the results here and explain it to us, since I certainly have no idea what the answer is
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

That's an incredibly valuable skill and one you'll absolutely need as your career begins.


Another point to make here is that these solutions are incredibly easy to what you will be facing as a professional programmer. Yes, it is true, it is easier to have an answer spoon fed to you than to look up the answer in the JavaDoc -- but keep in mind, you *can* look up the answer, and in one source.

When you are a professional programmer, your problem will be so specific that it is highly unlikely that you find a source for your answer directly. And the answer won't be in a JavaDoc -- it will be buried deep in tomes of books that you have to get up to speed on. If you feel that it is mean to send you to an easy organized source -- how are you going to do your job as a professional programmer?

IOWs, we are not being mean. The JavaDoc is easy. "researching" is a skill that you need to master. If we spoon fed you, so you never learned to correctly look up stuff, now, that would be mean.

Henry
[ June 26, 2008: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
M Marsden
Greenhorn

Joined: Jun 18, 2008
Posts: 18
Just curious, is anything similar to this question likely to be on the SCJP 1.5 edition? I've been studying for a while and haven't seen anything like this... How familiar with regexp should I be?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex
 
Similar Threads
questions regarding regular expressions
How greedy is "*"? Sometimes I don't see it greedy enough
Operators
When doing Matcher start on pattern \d* returned index seems off
regular expression