Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

Pattern extracting

Thread view: 
Joni - 23 Mar 2006 09:18 GMT
Hello

I have a string which has this format

    [TagName "Value"]

Is it possible to extract TagName and Value using the Pattern class ? If
yes how ? Something like:

    Pattern tagPattern = Pattern.compile("[.* \".*\"]"); // right pattern ????
    String[] val = tagPattern.????("[MyTagName \"Val\"]");

I would like to get

    val[0] = "MyTagName"
    val[1] = "Val"

I dont know if the Pattern class is the appropriate way to do this ?

Thanks.
Hendrik Maryns - 23 Mar 2006 13:40 GMT
-----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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.