如题,想把一个字符串分割成先要的部分:
STRING="change; alter[AAA; BBB; CCC]; modify; modification"
rtn_range = ("change","alter[AAA; BBB; CCC]", "modify", "modification")中间隔中括号比较烦人,麻烦各位高手了!!

解决方案 »

  1.   


    String str = "change; alter[AAA; BBB; CCC]; modify; modification";
    String[] tmp = str.split("(?<!.{0,100}\\[[^]]{0,100});");//数字自己调一下.适合自己的.python跑不过去.
    error: look-behind requires fixed-width pattern
      

  2.   

    这个可以[m.group() for m in re.finditer(r"(\w+(\[)?(?(2)[^]]*]))", STRING)] 适用我举的例子。我的例子是可以了,现在发现实际数据多少有些不同,有的是单词,有的是短语。
    还有中括号前有时候是空格。
    例如:
    STRING="lose presence of mind; all in [into] a fluster; be nervous and flustered; be puzzled [perplexed; upset]; be thrown into confusion; AAA[CCC; DDD]; BBB"
    麻烦再给加工加工,谢谢。
      

  3.   

    import reSTRING="change; alter[AAA; BBB; CCC]; modify; modification"
    rtn_range=re.split(';\s*(?![^\[]+\])', STRING)
    print rtn_range