my dog learned polymorphism*
The moose likes Java in General and the fly likes regex question, help please! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex question, help please!" Watch "regex question, help please!" New topic
Author

regex question, help please!

Araad Izadkhah
Greenhorn

Joined: Nov 07, 2005
Posts: 15
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

Joined: Apr 14, 2004
Posts: 10336

Ah, if only the String were like this:

or this:

it would be easy...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
Consider Paul's rocket mass heater.
 
subject: regex question, help please!