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 / General / November 2005

Tip: Looking for answers? Try searching our database.

need help with regex

Thread view: 
vamp4l - 04 Nov 2005 23:41 GMT
say i have a string as follows and i want to get what is between the
tags

String t = "<title> My First Page </title>";
String[] title2 = t.split(".+");
for(String s2 : title2)
    outString += s2;

im pretty sure using the split method is the way to go but i cant
figure out how to set up the regex
hiwa - 05 Nov 2005 01:54 GMT
> using the split method is the way to go
Is it? I don't think so.
Try this:
--------------------------------------------------------------------------
public class RegxTest{
 public static void main(String[] args){
   String s1 = "<title lang=\"en\"> My First Page </title>";
   String s2 = "<title>[foofoo]barbar</title>";
   String s3 = "<title>(barbar)foofoo</TITLE>";
   String regex = "(?i)(<title.*?>)(.+?)(</title>)";

   System.out.println(s1.replaceAll(regex, "$2"));
   System.out.println(s2.replaceAll(regex, "$2"));
   System.out.println(s3.replaceAll(regex, "$2"));
 }
}
-------------------------------------------------------------------
Roedy Green - 05 Nov 2005 08:50 GMT
>String t = "<title> My First Page </title>";
>String[] title2 = t.split(".+");
[quoted text clipped - 3 lines]
>im pretty sure using the split method is the way to go but i cant
>figure out how to set up the regex

your splitter is the string <title> or </title>

so for a first cut you might try "<title>|</title>"

But < is a reserved character , so I quote it, and > as well for
symmetry:

"\\<title\\>|\\</title\\>"

Remember to trim you strings and throw out "".

Another approach that will help ignore more stuff is to write finder
regex, then possibly a matcher regex on what you found.

See http://mindprod.com/jgloss/regex.html

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



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



©2009 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.