在python字符串中查找括号

2024-04-20 11:37:13 发布

您现在位置:Python中文网/ 问答频道 /正文

这儿那儿

我现在有一个字符串如下

location1
      {
        type uniform;
        axis y;
        start (1000 -300 0.05 );
        end   (1000 9 0.05 );
        nPoints 3000;
      }

我叫它locationString。你知道吗

我试图找到这个字符串中所有括号(都是“(”&;“)”)的所有索引,如下所示

 import re
 kwSe='('
 kwEe=')'
 kwS=[match.start() for match in re.finditer(kwSe,str(locationString))]
 kwE=[match.start() for match in re.finditer(kwEe,str(locationString))]

但这不管用,有人能帮忙吗?你知道吗


Tags: 字符串inrefortypematchuniformstart
1条回答
网友
1楼 · 发布于 2024-04-20 11:37:13

^{}接受regex模式,而parentheses have special meaning接受regex模式(它们形成组)。如果要匹配文字(),必须对它们进行转义(如\(\)),或者将它们括在字符类中(如[(][)])。你知道吗

import re
kwSe='\('
kwEe='\)'
kwS=[match.start() for match in re.finditer(kwSe,str(locationString))]
kwE=[match.start() for match in re.finditer(kwEe,str(locationString))]

相关问题 更多 >