Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex question, help please!

 
Araad Izadkhah
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,
i need help with the following:
i have a string like this:
<logout reason="user logged out" time="12:32 2006">
and i want to split this string (using split("\\s")), but i want it to give me tokens like this:

reason
"user logged out"
time
12:32
2006

if i use split("\\s\"") it only gives me 1 token:
reason="user logged out" time="12:32 2006"

which is basically back to square one!
PLEASE HELP
thx in advance
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, if only the String were like this:

or this:

it would be easy...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate a little bit, I believe Paul is suggesting that if the String represents valid XML (which would necessarily include an end tag), then it would be easiest to use some sort fo XML parser. If it's not valid XML, then I guess you can use regular expressions, but the split() method probably won't help much as the delimiters are too different. I'd recommend using a Pattern and Matcher to find the whole tag, and then another Pattern and Matcher to locate the individual attributes within the tag. This will require some knowledge of regular expressions however. And there are some complications which may not be immediately obvious. If you can use a standard XML parser, that will probably be simpler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic