File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression

 
David Coppens
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Coppens
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 272
C++ Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Coppens
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can the field names also contain spaces? If not, then it should be possible w/o much effort.
 
David Coppens
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, field names can not contain spaces.

What would your advice be given this knowledge?

Thanks a lot for your help.
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Coppens
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ulf, this one is even more general.

I will be crediting you guys in the code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic