-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message
Joni schreef:
> Hello
>
[quoted text clipped - 7 lines]
> Pattern tagPattern = Pattern.compile("[.* \".*\"]"); // right
> pattern ????
Well, apart from the [], it does match the input, but does nothing with it.
> String[] val = tagPattern.????("[MyTagName \"Val\"]");
>
[quoted text clipped - 4 lines]
>
> I dont know if the Pattern class is the appropriate way to do this ?
You need a matcher too. In particular, look at the grouping constructs,
and the group(int) method in Matcher.
Pattern tagPattern = Pattern.compile("\[(\S*)\s+\"(.*)\"\]");
// you can replace \s+ by ' ' if you're sure it is exactly one space.
// be careful when replacing \S*, if you use .*, you'll probably need a
// non-greedy quantifier.
Matcher match = tagPattern.matcher(inputString);
String tag = match.group(1);
String value = match.group(2);
HTH, H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
Oliver Wong - 23 Mar 2006 18:55 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 6 lines]
>>
>> [TagName "Value"]
[...]
> Pattern tagPattern = Pattern.compile("\[(\S*)\s+\"(.*)\"\]");
> // you can replace \s+ by ' ' if you're sure it is exactly one space.
> // be careful when replacing \S*, if you use .*, you'll probably need a
> // non-greedy quantifier.
What about the second .* that appears between the double-quote marks?
Shouldn't that be non-greedy as well (or replaced with \S* as well)?
- Oliver
Hendrik Maryns - 23 Mar 2006 19:45 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message
Oliver Wong schreef:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
[quoted text clipped - 16 lines]
> What about the second .* that appears between the double-quote marks?
> Shouldn't that be non-greedy as well (or replaced with \S* as well)?
Hm, I guess you're right. I never really got the grasp of greedy vs.
non-greedy. Though \S* would be wrong here, it would rather need to be
something like [^"] (not sure whether the " needs to be escaped there).
And then again, this will give you problems with matching braces etc.
It is of course improbable that the Value will contain "], but you never
know... Regexps are not really appropriate to solve nested groupings.
There are Perl packages for this, though...
And the Perl documentation is really interesting recommendable if you
want to learn regexps, especially because almost all of Perl's regexp
functionality is implemented by Pattern (see the Javadoc for where it
doesn't).
H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org