• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt regarding pattern matching in java  RSS feed

 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir/Madam

PFB the code I had typed in ECLIPSE IDE:
import java.util.regex.*;

public class TestClass {


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.println(m.start()+m.group());
}


}

}

I passed the command line arguments as:

java TestClass "\d*" ab34ef

I am getting blank output on the console. But in the book it says that the output will be 01234456. Please explain why.

Regards
Mansukhdeep
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

What's "PFB"?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run this code fragment from the command-line it should work as expected for those arguments.
Since you're using Eclipse, Im assuming you're also running this code from inside the IDE using a run configuration that passes in those arguments.
That's what's tripping you up here. Run it again from inside the IDE, put a breakpoint at the start of your main method and inspect the values in the args array.
Not what you'd expect is it?

Edit: Oh, you ARE running it from the command-line, hmm.
Well then you must not be using the double quotes to surround the regex value, which will present the same problem as the Eclipse run configuration would.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add this line to your code and see what it says:



Hunter
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running this code from Eclipse - and command line gives me this result
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

What's "PFB"?


Apologies David
Will take care of the code tags next time onwards. PFB is the short form of "Please find below".
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:add this line to your code and see what it says:



Hunter


Hi Hunter
It is returning false. I do not quite understand why?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is returning false because your pattern is not matching. I ran into this same problem and had to change your regex, which is weird because Rene said that your regex worked fine when she ran your code. Try this: "[\d]*"

Hunter
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:
David Newton wrote:What's "PFB"?


PFB is the short form of "Please find below".

Please UseRealWords. Only use globally accepted acronyms. This is the first time I've seen PFB.

Mansukhdeep Thind wrote:
Hunter McMillen wrote:add this line to your code and see what it says:



Hunter


Hi Hunter
It is returning false. I do not quite understand why?

If I literally copy-paste the code from your start post and run it as described, including the quotes around \d*, then I see the desired output.

Can you perhaps put the following line at the top of your program:
Perhaps your arguments are not what you expect. For instance, when you forget the quotes around \d* then the shell will interpret it first. When I first forgot it listed all files and folders in my root folder that start with d.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Mansukhdeep Thind wrote:
David Newton wrote:What's "PFB"?


PFB is the short form of "Please find below".

Please UseRealWords. Only use globally accepted acronyms. This is the first time I've seen PFB.

Mansukhdeep Thind wrote:
Hunter McMillen wrote:add this line to your code and see what it says:



Hunter


Hi Hunter
It is returning false. I do not quite understand why?

If I literally copy-paste the code from your start post and run it as described, including the quotes around \d*, then I see the desired output.

Can you perhaps put the following line at the top of your program:
Perhaps your arguments are not what you expect. For instance, when you forget the quotes around \d* then the shell will interpret it first. When I first forgot it listed all files and folders in my root folder that start with d.


I've since tried running the program and that's exactly the behaviour I'm seeing as well - from the command-line that is.
Eclipse, however, seems intent on ignoring double quotes for arguments passed via the run configuration.
The only way I got it to show the expected output in Eclipse's console, was to alter the regular expression and make the entire thing a capturing group i.e. (\d*)
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:It is returning false because your pattern is not matching. I ran into this same problem and had to change your regex, which is weird because Rene said that your regex worked fine when she ran your code. Try this: "[\d]*"

Hunter

Yeah, that is strange - am I the only one who can run this code, with the given pattern ??

@Hunter
BTW: I'm a he
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . Only use globally accepted acronyms. This is the first time I've seen PFB. . . .
The globally-accepted acronym is, of course, vi.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's odd because I was printing out the command-line args like you said Rob and for some reason when I typed:



Inside my program when I printed out the args I was getting:



It wasn't until I changed "\d*" to "[\d]*" that I received the correct output.

Hunter
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up globbing.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!