Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parse text between angle brackets

 
Hernan Eche
Greenhorn
Posts: 6
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not simple but i can give an erroneous one...
 
Hernan Eche
Greenhorn
Posts: 6
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48952
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are still using a regex, even though it is a simple one "<".
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can simply indexof everything you need, if you're interested in not using regex.
As simple as that:
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48952
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
C++ Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic