String parsing functionality (or the lack of) in Java

A user would like to parse for:

name : status

value : passed

in the following string:

name : status

value : passed

name : status

value : operational

name : result

value : passed

In Perl, it would be: if ( str =~ /name : status\nvalue : passed/) { return true }

How would one do it in Java? Mind the '\n' (newline) at the end of each line.

[414 byte] By [bai13ysa] at [2007-11-27 11:29:39]
# 1

if (str.indexOf("name : status\nvalue : passed") >= 0) return true;

ordinary_guya at 2007-7-29 16:29:14 > top of Java-index,Java Essentials,Java Programming...
# 2

You're absolutely right.

Now, let's alter it a little. What if the string consists of the following XML elements taken out of an XML file?

<output>

<name>status</name>

<value>passed</value>

</output>

How would you do in Java what this Perl code does? Pretend there are multiple spaces before each tag.

if (str =~ /<name>status</name>[\n\r]\s+<value>passed</value>/) return true;

Message was edited by:

bai13ys

Message was edited by:

bai13ys

bai13ysa at 2007-7-29 16:29:14 > top of Java-index,Java Essentials,Java Programming...
# 3

whats stopping you from reading a regex tutorial or looking

up one of the many java xml parsers?

http://www.google.com/search?hl=en&safe=off&q=java+xml

TuringPesta at 2007-7-29 16:29:14 > top of Java-index,Java Essentials,Java Programming...
# 4

Java XML parser is too complicated for parsing a simple name-value pair under the same parent. I don't want to use 20+ lines of code with which 10 of those being DOM document setup code. I'm pretty sure it's doable, though. I just think it's way too tedious for a simple thing like this.

Thanks, I missed the Matcher.find() method. That did the trick.

bai13ysa at 2007-7-29 16:29:14 > top of Java-index,Java Essentials,Java Programming...
# 5

> if (str =~ /<name>status</name>[\n\r]\s+<value>passed</value>/) return true;

That's not valid even in Perl; you didn't escape the slashes in the end tags, and [\n\r]\s+ is redundant, since \s matches linefeed and carriage return. Anyway, in Java you would do something like this this: Pattern p = Pattern.compile("<name>status</name>\\s+<value>passed</value>");

Matcher m = p.matcher(str);

return m.find();

uncle_alicea at 2007-7-29 16:29:14 > top of Java-index,Java Essentials,Java Programming...