Results 1 to 10 of 10
Thread: Help needed with String.split()
- 02-02-2011, 07:44 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Help needed with String.split()
Hi
I'd like to split a String with the following rule : Parse everything between a "( )" parenthesis besides nested parenthesis, meaning:
String s = "(example) (ex) (am (p (k) t) le)"
needs to be parsed into :
example
ex
am (p (k) t) le
I tried to work with split() and regular expressions but couldn't find the right one.
Thanks in advance:)
- 02-02-2011, 08:05 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
-
- 02-02-2011, 08:08 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
-
- 02-02-2011, 08:17 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Thanks , I know that a full parsing of nested parenthesis can't be done with regular expressions but please note that my demand is much simpler and doesn't involve counting - I'm saying : "parse everything inside a parenthesis accept if it's inside other parenthesis - i don't care how many " - to be clear :
String s = "(I) (don't) (care)" should give : [I , don't , care];
String s = (I) (do (n'(t c) a) (re) should give: [I, do (n'(t c) a, re;
Is this equivalent hard as the general task ?
Thanks
- 02-03-2011, 12:58 AM #7
- 02-03-2011, 03:12 AM #8
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
@rtaig, this is toy example
Java Code:String s = "(I) (don't) (care)"; int c = 0; String joiner = ""; for(int i =0 ;i <s.length(); i++){ if ( c!= 0 ){ joiner+=s.charAt(i); }else if ( c==0 && joiner !="" ){ System.out.println ("joiner: " + joiner); joiner=""; } if ( s.charAt(i) == '(' ){ c++; }else if ( s.charAt(i) == ')' ) { c--; } } System.out.println("joiner: " + joiner);
- 02-03-2011, 03:14 AM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
If you will not use a counting then the result will be
which is different from your expected output:
INPUT:
String s = (You) (are (no (t li) st) (ening)
POSSIBLE OUTPUT: [You, are not li st, ening];
INPUT:
String s = (You) (are (no (t li) st) (ening)
OUTPUT: [You, are (no(t li) st, ening];
- 02-03-2011, 07:16 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
It does involve counting; this morning I tried to come up with a simple proof that parsing balanced parentheses can't be done with regular languages but I failed. I always fall back to the pumping lemmas which show that the language of balanced (nested) parentheses is a context free language; but then again, I didn't have my coffee yet and I'm going to fix that right now ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
Split string help
By Ben1 in forum New To JavaReplies: 1Last Post: 11-08-2010, 04:28 PM -
String Split
By sarovarc in forum New To JavaReplies: 6Last Post: 04-19-2010, 05:06 AM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks