File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String Extraction using regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Extraction using regex" Watch "String Extraction using regex" New topic
Author

String Extraction using regex

Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
Hi,

I have a multi line status xml out of which i need to extract the status value.
<status>ok</status>
<status>Failed</status>

i cannot parse it as a xml and i have to use a pattern to extract the "ok" or"failed"(or it could be any other error code which i dont know in advance). can some one please help me to extract the value? I am a novice in regex and googling did not help me much.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

I am a novice in regex


How much regex do you know? Do you, at least, know the basics?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
Yes just the basics. I intend to leArn and use it more though.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Mahesh Mak wrote:Yes just the basics. I intend to leArn and use it more though.


Assuming that you know how to do basic matching -- matching the <status> tags along with the meta text in-between (using the find() method).... To extract the text in-between, setup a subgroup for it. You can then get the string with the group() method. See the java.util.regex.Pattern class for more info. See the section about groups and capturing.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Henry Wong wrote: . . . See the java.util.regex.Pattern class . . .
The Java™ Tutorials section is another good resource.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Mahesh Mak wrote:Hi,

I have a multi line status xml out of which i need to extract the status value.
<status>ok</status>
<status>Failed</status>

i cannot parse it as a xml ...


That seems a contradiction: you have XML, but you can't parse it as XML...
Perhaps you meant: "I don't want to parse it as XML". Out of curiosity: why not?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Extraction using regex