aspose file tools*
The moose likes Java in General and the fly likes Parse text between angle brackets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parse text between angle brackets" Watch "Parse text between angle brackets" New topic
Author

Parse text between angle brackets

Hernan Eche
Greenhorn

Joined: Aug 01, 2011
Posts: 6

String myProblemIs = "I would like to parse <the first appearance> of a text surrounded by < and >, doesn't mather with a <third> and a <forth> appearance etc.. (with the first is enough)";

resultShouldBe = "the first appearance";

/* It seems a simple problem, so is there a simple way of doing it with/without regex? */

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
not simple but i can give an erroneous one...
Hernan Eche
Greenhorn

Joined: Aug 01, 2011
Posts: 6

Why do you say it's an erroneous one? It seems simple and right =) +1

John Jai wrote:not simple but i can give an erroneous one...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
You are still using a regex, even though it is a simple one "<".
Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

you can simply indexof everything you need, if you're interested in not using regex.
As simple as that:
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
nice campbell..

hernan - it will still print a value if the input is like "abcd <only opening bracket present".... won't it? and worst will be "abcd < efgh <caught here>"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
If you use indexOf you need to be careful not to get an IndexOutOfBoundsException by trying to split on -1.

But that technique does not use a regex.
Hernan Eche
Greenhorn

Joined: Aug 01, 2011
Posts: 6

John Jai wrote:nice campbell..

hernan - it will still print a value if the input is like "abcd <only opening bracket present".... won't it? and worst will be "abcd < efgh <caught here>"


You are right, I could filter it previously, both brakets Must be in string, and in second case, that would include an ugly "<", but anyway would respect definition of "between angle brackets"

thanks for giving a deeper insight

Campbell yes, that is a kind of regex, anyway with regex I meant to Java specific classes, and I would use Matcher, Pattern class, etc.. but I just have the prejudice that (for simple cases like this) they makes the program difficult to read.
 
 
subject: Parse text between angle brackets