Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

doubt regarding pattern matching in java

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add this line to your code and see what it says:



Hunter
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running this code from Eclipse - and command line gives me this result
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22645
123
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75638
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look up globbing.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic