• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string extraction using regular expressions

 
pavithra murthy
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends .

i wanted to read a string from text file "MRN 123456789" . This text is actually obtained from or extracted from a pdf file to text file using pjtext jar file . while extracting to text file it will be in its own format .
i have written a code to extract the above number using regular expressions .
But since the text file contains words like "MRN 123456789E0Liste der Positionen" .this is also displayed on the command prompt indicating the string has been matched according to regular expression.

but i want only MRN 123456789 . how will be able to do this .
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that the _exact_ pattern you are looking for? Or are there other possibilities?
 
Vinu Valayil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pavithra,

As Martijn said, if this is the exact pattern you're looking for, then you could use:



If you know the exact number of digits appearing in that number, you could also use

 
santosh patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pavithra

"MRN\\s\\d+" patther will match for MRN 123456789

You can print Matcher object to check lastmatch result.

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(line);
boolean found = m.lookingAt();

System.out.println(m.toString());
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic