| 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: 16811
|
|
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: 16811
|
|
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: 32830
|
|
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?
|
 |
 |
|
|
subject: String Extraction using regex
|
|
|