• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex Doubt?

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a String as below:


I want to capture just FP009Y260KP from the above string. I tried with regex But it says no match found.
[ April 03, 2008: Message edited by: Gopu Akraju ]
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this works perfectly fine for me.

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried as below:



It is just printing as below:
java.util.regex.Matcher[pattern=FP.*KP region=0,170 lastmatch=]

What does it mean? How to print FP009Y260KP alone from the test string?
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anand. Both of us should have posted almost the same time. I tried as you mentioned.
System.out.println(matcher.find());
It is just printing trueBut I want to print the string FP009Y260KPThanks again.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.



I suggest that you should read some good regex tutorials. That will help you enrich your knowledge. Do some googling. That should do.

Also, get the differnce between the methods like matcher.find() , matcher.lookingAt() and matcher.matches(). These subtle differences are really important.
[ April 03, 2008: Message edited by: Anubhav Anand ]
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Just pointing out the obvious... isn't calling loc_matcher.group() much much easier than calling subtring() with the start() and end() of the match?

Henry
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very true Henry. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic