Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Extraction using regex

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes just the basics. I intend to leArn and use it more though.
 
Henry Wong
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79007
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote: . . . See the java.util.regex.Pattern class . . .

The Java™ Tutorials section is another good resource.
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic