• Post Reply Bookmark Topic Watch Topic
  • New Topic

regular expression question  RSS feed

 
jonathan ford
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the example string: "(This is a) great (example for RE)"
I'm trying to use string.split(), to make string[] looks like:
[0]: This is a
[1]: great
[2]: example for RE

I don't know how to write the regular expression. could someone can help me with this? thanks
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a tendency to send people to the Java Tutorials. Try that link.

I doubt greatly whether your task is possible with String#split.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by myself:
I doubt greatly whether your task is possible with String#split.


But I now think I was mistaken saying that; it should be possible to write a suitable regular expression.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a regex split() can definitely do this... but in my opinion, it can probably be done cleaner with a regex find() in a loop.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to write the regular expression. could someone can help me with this? thanks



But back to the question... IMO, the regular expression engine is not just a simple library. Heck, there are scripting engines just for regex. It can be considered as a language by itself.

If you don't know regex, the "help" is basically "we do it for you", which doesn't really help you learn. I suggest you take a shot at a regex tutorial first (as Campbell recommended).

Henry
[ September 11, 2008: Message edited by: Henry Wong ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is right; the regular expressions tutorial I quoted is very good.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Henry is right; the regular expressions tutorial I quoted is very good.


And you were also right in your first reply: using String.split(...) alone, it is not possible to get the output the OP posted in his original post. Because of the '(' at the beginning of the String, there will always be an empty String as the first element of the returned array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!