• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, what Ulf said.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
M Marsden
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic