regex - Java: which parser is best to match text according to parenthesis pairs? -


string example:

"(this level 1. (this level 2. (this level 3.) level 2.) level 1.)"

if not known in advance how many levels of parentheses there (i.e. there level 4, 5, 6 etc. within level 3), understanding is not possible regex match level 2 text in above example. so, example, not possible match following using regex:

"(this level 2. (this level 3.) level 2.)"

i have read using parser i'm not sure best. looking ease of use, speed, , compatibility java. on appreciated.

i did myself last year.

it easy, start searching last ( , store index. search first ) after index stored. need substring extract text between these 2 index , done :)

for next level, use previous index find previous/next (/ ) , have easy parse ;)

here sample of simple code print each level :

public static void main(string... args) {         string s = "(this level 1. (this level 2. (this level 3.) level 2.) level 1.)";         searchinto(s);     }      public static void searchinto(string s){         searchinto(s, s.length(), 0);     }      public static void searchinto(string s, int from, int to){         int nextopen = s.lastindexof("(", from-1);         if(nextopen >= 0){             int nextclose = s.indexof(")", to);              system.out.println(s.substring(nextopen + 1, nextclose));             searchinto(s, nextopen, nextclose + 1);         } else             return;     } 

will print :

this level 3. level 2. (this level 3.) level 2. level 1. (this level 2. (this level 3.) level 2.) level 1. 

edit :

this should cover more ground. search internal block , block near each other.

public static void main(string... args) {         searchinto("(this level 1. (this level 2. (this level 3.) level 2.) level 1.)");         searchinto("(l1) (l2) (l3)");         searchinto("(l1) (l2 (l2a)) (l3)");     }      public static void searchinto(string s){         searchinto(s, s, s.length(), 0);     }      public static void searchinto(string s, string original, int from, int to){         int nextopen = s.lastindexof("(", from);         if(nextopen >= 0){             int nextclose = s.indexof(")", nextopen);             string tmp = original.substring(nextopen + 1, nextclose);             system.out.println(tmp); //print result, use list store or treat directly.             s = s.substring(0, nextopen) + "#" + s.substring(nextopen + 1, nextclose) + "#" + s.substring(nextclose + 1);             searchinto(s, original, nextopen - 1, nextclose + 1);         } else             return;     } 

will output :

this level 3. level 2. (this level 3.) level 2. level 1. (this level 2. (this level 3.) level 2.) level 1.  l3 l2 l1  l3 l2a l2 (l2a) l1 

by using alternative string replace found character, prevent code stuck on index. print value original print correct value (without #character used replace ( ) found)

this bit more expensive if need cover every structure.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -