• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

string extraction using regular expressions  RSS feed

 
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 .
 
author
Bartender
Posts: 3280
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?
 
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

 
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());
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!