wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regular expression" Watch "Regular expression" New topic
Author

Regular expression

David Coppens
Greenhorn

Joined: Oct 24, 2007
Posts: 17
Hi I have the following string:

name : value othername : longer value newname : value

I want to split this string into three sections:
name : value
othername : longer value
newname : value

But I can not think of a regular expression that will do this.
Currently I am stuck with this:

This almost does the job, but the second pair becomes
othername : longer (value is not there..)

Any suggestions?


SCJA (94%)<br />SCJP (84%)<br />SCWCD (84%)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
If the character that separates the name/value pairs from each other can also occur within a value, then I don't see much chance of this, with or without regexps.

Could you change the format to use a different separator character, maybe a tab?


Ping & DNS - my free Android networking tools app
David Coppens
Greenhorn

Joined: Oct 24, 2007
Posts: 17
No, I do not control these strings.

The only thing that is always true is the following (which is not clear
from my example):

name: value longname: this is longer again: shorter

So, what I am looking for is an expression like this:

a word followed directly by : followed by anything but 'word:'

But I can't seem to find a possibility to say 'anything except some expression'.
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

Are you particular about a regular expression? Or would it suffice for you to get the job done?

Why not search for "Name : " and "Othername : " (or whatever the names of your fields are). One plus the end of the first search pattern and the beginning of the next search pattern give you the start and end iterators for the substrings that represent the values of your fields.

- Anand


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
David Coppens
Greenhorn

Joined: Oct 24, 2007
Posts: 17
I also do not know what the field names could be.

It could be anything. All I know is the structure.

Seems to be difficult. I think I might have to write my own parser and just go through the string one character at a time.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Can the field names also contain spaces? If not, then it should be possible w/o much effort.
David Coppens
Greenhorn

Joined: Oct 24, 2007
Posts: 17
No, field names can not contain spaces.

What would your advice be given this knowledge?

Thanks a lot for your help.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Originally posted by David Coppens:
No, field names can not contain spaces.

What would your advice be given this knowledge?


David,

With this knowledge, regular expressions can do this with ease.

Unfortunately, there is little chance that giving a hint will work here -- if you don't know regular expressions. So... here is a possible answer.



Furthermore, I would really recommend that you pick up a good book on regular expressions -- and learn it -- as using this code without understanding it, is not a good idea.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Coppens
Greenhorn

Joined: Oct 24, 2007
Posts: 17
I am Master of Science in Computer Science, so I do know regular expressions.

However, I did not see an expression that would work for me.
That is because I did not know of the lookahead possibilities
Java provides in its regular expressions.

Thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Ah, too late to the game. I had come up with this, which doesn't assume that the colon immediately follows the name. I'm not sure if it is otherwise equivalent to Henry's solution.

[ November 09, 2007: Message edited by: Ulf Dittmer ]
David Coppens
Greenhorn

Joined: Oct 24, 2007
Posts: 17
Thank you Ulf, this one is even more general.

I will be crediting you guys in the code.
 
jQuery in Action, 2nd edition
 
subject: Regular expression
 
Similar Threads
Garbage collection
MindQ #36, regarding GC
Garbage collection
Rename a directory in Java
use of regular expression in java