0

pls help me.

how can I split this String value?

my code below wont work:

String[] tempStr;

toSplit = "This is the just a sample <FONT COLOR=BLACK>1)[option1/option2]</FONT> sentence. This is another sample <FONT COLOR=BLUE>1)[othertext1/othertext2]</FONT> sentence."

tempStr = toSplit.split("<FONT COLOR = [A-Z]*>([.])*</FONT>");

what i want guys is to split the paragraph with the delimiter .... thanks for any help.

6
  • is it for font color 'BLACK' or 'BLUE'? Commented Nov 10, 2011 at 8:33
  • What exactly is not working? Does it not compile or does it return unexpected results? What is the compilers error message or what is returned at runtime? Commented Nov 10, 2011 at 8:39
  • Would you like to output the font colors? Because .split() only captures the string around the regexp matches. Commented Nov 10, 2011 at 8:40
  • it wont return the expected results or the String was not split. Commented Nov 10, 2011 at 8:41
  • Maybe you should take a look at pattern and matcher: leepoint.net/notes-java/data/strings/40regular_expressions/… Commented Nov 10, 2011 at 8:43

1 Answer 1

4

It depends how would you like to split. If you need to extract the text fragments as a array elements and remove all HTML tags you can say something like the following:

tempStr = toSplit.split("<FONT.*?</FONT>");

Pay attention. I do not write all attributes of FONT tag. It is not needed here. My expression is simpler and do not have to care about spaces, quotes etc.

But this regex is case sensitive. To be more flexible and be able to use various flags supported by Pattern use java.util.Pattern, create Matcher and then use its split() method.

BTW if your parsing may be more complicated I'd recommend you to use "real" html parser.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.