[英]Python Regex to capture whats in between
i'm trying to write a regex which will get the following:我正在尝试编写一个将获得以下内容的正则表达式:
So far i have written the below which works kinda well except that i'm not sure how to get it to remove "(" or ":" or ","到目前为止,我已经写了下面的内容,效果还不错,只是我不确定如何删除“(”或“:”或“,”
def get_name(line):
subStr = re.findall(r'Dim(.+)As|$', line)[0]
return subStr
Example例子
txt = "Dim MyLine() As"
subStr = re.findall(r'Dim(.+)As|$', txt)[0]
#Prints MyLine(), But I want it to print MyLine
Same for the cases "Dim MyLine:" "Dim MyLine As" "Dim MyLine," "Dim MyLine() As"对于“Dim MyLine:”“Dim MyLine As”“Dim MyLine”,“Dim MyLine() As”的情况相同
I Want to get only "MyLine" from all the above cases, and only if they occur.我只想从上述所有情况中获取“MyLine”,并且仅当它们发生时。
I'm not sure what do you mean by return nothing, so I will assume you want to return None
.我不确定什么都不返回是什么意思,所以我假设您想返回
None
。
>>> import re
>>>
>>>
>>> def get_name(line):
... try:
... return re.findall(r'Dim\s*(.+?)([(: ,]|$)', line)[0][0]
... except IndexError:
... return None
...
>>> get_name('Dim MyLine() As')
'MyLine'
If there is no match, the findall
returns an empty list, so if we want to access the first element, that will raise IndexError
, we catch that, and return None
.如果没有匹配项,则
findall
返回一个空列表,因此如果我们想要访问第一个元素,那将引发IndexError
,我们捕获它并返回None
。
Regex: Dim\s*(.+?)[(: ,]
正则表达式:
Dim\s*(.+?)[(: ,]
\s*
: any number of whitespace character \s*
:任意数量的空白字符(
: first group starts (
: 第一组开始.+?
: At least one time any character (non-greedy matching) )
: first group ends )
: 第一组结束[(: ,]
: One of these characters: (: ,
[(: ,]
:这些字符之一:( (: ,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.