2013年11月4日星期一

Find regex split string ideas

A string : str = "(1) P ((2) @!)";
expect split into an array of array
arr [0] = (1)
arr [1] = P
arr [2] = (2)
arr [3] = @!

What should I do ?
supplement ( 1 ) as a whole , which can be ( 3 ) or ( 50 ) , etc.
Thank
Online

------ Solution ---------------------------------- ----------
this can not be used if it is to use the stack
scan strings
encounter encounter letters push ( push
encounter ) popped see is not ( if not then play until you find (
Then look at the stack is not ( not on the bomb until it encounters (

general idea is this
------ Solution -------------------------------- ------------
way to try with the matching
	public static void main(String[] args) {
String str = "(1)  P ((2)@!)";
Pattern p = Pattern.compile("(\\(\\d+\\))\\s*(\\w+)\\s*\\((\\(\\d+\\))(.+)\\)");
Matcher m = p.matcher(str);
String[] arr = null;
if(m.find()) {
 arr = new String[]{m.group(1),m.group(2),m.group(3),m.group(4) };
}
for(String s : arr) {
System.out.println(s);
}
}

------ Solution ------------------------------------- -------
Pattern.compile ("(\ \ (\ \ d + \ \)) \ \ s * (\ \ w +) \ \ s * \ \ ((\ \ (\ \ d + \ \ ) ) ( . + ) \ \ ) " ) ;

above can be grouped in brackets , one match.

没有评论:

发表评论