Hi,
I have been searching on the Internet about how to solve my problem. I
have a string that needs to be split using regexp in ruby. The problem
is that I cannot find a way to get it right. The string has a fix
format. The rules for matching are as followed:
1)A string starts and end with any character string
2)Matching string starts from "<<" and end with ">>"
3)Matching string must be a pair of "<<" and ">>" and must be nested
correctly
4)There may be any unwanted string between matching pairs but never be
inside the pairs
A sample string is as followed:
str = "<<p1 <<p2>> <<p3 <<p4>>>>>> <and> <<p5>> or <<p6 <<p7 <<p8>>>>>>"
The result string I am looking for from the above string should be as
followed:
["<<p1 <<p2>> <<p3 <<p4>>>>>>", "<<p5>>", "<<p6 <<p7 <<p8>>>>>>"]
The closest I could come up with is str.split(/(<<[^>].*(>>)+)/) and I
got the result as followed:
["", "<<p1 <<p2>> <<p3 <<p4>>>>>> <and> <<p5>> <<p6 <<p7 <<p8>>>>>>",
">>"]
I cannot change the rule (using "<<" and ">>), so I had little
experience on how to match 2 consecutive characters. It would be
appreciated if anyone has an input or suggestion. Thank you.
Chai
···
--
Posted via http://www.ruby-forum.com/.