File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
RabbitMQ in Depth
this week in the
Java in General
Pattern matching problem
Joined: Oct 01, 2008
Aug 26, 2009 10:18:39
I am trying to retrive the body contents and file tag conetents if any.
str = "<body>1</body><file>myFile1</file><body>2</body><body>3</body><file>myFile2</file>";
pattern = Pattern.compile("<body>(.*?)</body><file>(.*?)</file>");
Matcher matcher = pattern.matcher(str);
I am getting the output in this way
See, mu BValue is not coming properly, I want output in following way...I know this is not in proper pattern..but still can we achieve this output?
Joined: Feb 23, 2009
Aug 26, 2009 10:21:36
I am not a fan of engaging the heavy regex engine when string positions would do just fine, as they do in your case.
You could rewrite your program using code.indexOf("<body") etc. and loop over all occurances.
An easy to use, light-weight JDBC framework
Joined: Oct 27, 2005
Aug 26, 2009 12:07:30
The problem is that your regular expression requires a <file> to come after an <body>. That's why the non-greedy .*? will match "2</body><body>3". To prevent this, make the <file> optional.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
I agree. Here's the link:
subject: Pattern matching problem
Tokenizing string in Java
Regular Expression Query
Need help in regular expression
Regular Expression - Set of Values
Regular expression to take integers out of a string
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015