need help with regex

V

vamp4l

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
 
H

hiwa

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"));
}
}
-------------------------------------------------------------------
 
R

Roedy Green

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top