Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression

 
yathish Gatty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

When I try to match a special character with the regular expression it hangs.

The code for regular expression matching is:
Pattern pattern=Pattern.compile("abc");
Matcher match=pattern.matcher("--------------");
if(match.find()){//it hangs here
}

With regards,

Yathish
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "it hangs"? Do you get an error message, or is the output different from what you expect? Or does the program lock up? I tried your code with a small test program:

It prints "Two", exactly as expected.

Please explain your problem more clearly.
 
yathish Gatty
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Regular expression locks up.And it fails to terminate and does not throw any error.Is there some means by which I can exit out when the java util hangs.

With regards,

Yathish
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, make your question clearer. State what you expect and how that does'nt happen?

As mentioned by Jesper the code seems to work perfectly.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by yathish Gatty:
hi,
Regular expression locks up.And it fails to terminate and does not throw any error.Is there some means by which I can exit out when the java util hangs.

With regards,

Yathish


As Jesper explained, the above code does not cause the program to "hang". Since Java has no control over whether your computer freezes or not, there is no way it can determine when to exit. Besides, if the computer is locked up, it wouldn't be able to execute the code to make your program exit anyway.

With that said, I suspect there is some other cause of your problem. Can you provide more details about what happens when you run this program. It will probably be helpful if you describe how you run the program (i.e. using an IDE or the command line).

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you should add System.out.println() calls in order to find more about what is happening in your program.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic