Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Extraction using regex

 
Mahesh Murugaiyan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a novice in regex


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

Henry
 
Mahesh Murugaiyan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes just the basics. I intend to leArn and use it more though.
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote: . . . See the java.util.regex.Pattern class . . .
The Java™ Tutorials section is another good resource.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic