在文档字符串中提取多个变量名称
Extract multiple variable names in docstrings
我正在尝试从 Python 中查找文档字符串中的所有变量名称。例如,文档字符串的形式如下:
Scans through a string for substrings matched some patterns (first-subgroups only).
Args:
text: A string to be scanned.
patterns: Arbitrary number of regex patterns.
Returns:
When only one pattern is given, returns a string (None if no match found).
When more than one pattern are given, returns a list of strings ([] if no match found).
我想用正则表达式提取 text
和 patterns
。
由于这个特定的正则表达式,我尝试使用此代码查找以 :
结尾的断行之后的所有元素:
string = """Args:
text: A string to be scanned.
patterns: Arbitrary number of regex patterns."""
print(re.findall('Args:[\r\n]+(.+?):', string))
但是这个正则表达式没有捕捉到任何东西,我做错了什么?
我会使用 docstring-parser
而不是重新发明轮子。它支持 Google、ReST 和 Numpydoc 风格的文档字符串。
from docstring_parser import parse
s = """
Scans through a string for substrings matched some patterns (first-subgroups only).
Args:
text: A string to be scanned.
patterns: Arbitrary number of regex patterns.
Returns:
When only one pattern is given, returns a string (None if no match found).
When more than one pattern are given, returns a list of strings ([] if no match found).
"""
doc_str = parse(s)
print([param.arg_name for param in doc_str.params])
输出
['text', 'patterns']
我正在尝试从 Python 中查找文档字符串中的所有变量名称。例如,文档字符串的形式如下:
Scans through a string for substrings matched some patterns (first-subgroups only).
Args:
text: A string to be scanned.
patterns: Arbitrary number of regex patterns.
Returns:
When only one pattern is given, returns a string (None if no match found).
When more than one pattern are given, returns a list of strings ([] if no match found).
我想用正则表达式提取 text
和 patterns
。
由于这个特定的正则表达式,我尝试使用此代码查找以 :
结尾的断行之后的所有元素:
string = """Args:
text: A string to be scanned.
patterns: Arbitrary number of regex patterns."""
print(re.findall('Args:[\r\n]+(.+?):', string))
但是这个正则表达式没有捕捉到任何东西,我做错了什么?
我会使用 docstring-parser
而不是重新发明轮子。它支持 Google、ReST 和 Numpydoc 风格的文档字符串。
from docstring_parser import parse
s = """
Scans through a string for substrings matched some patterns (first-subgroups only).
Args:
text: A string to be scanned.
patterns: Arbitrary number of regex patterns.
Returns:
When only one pattern is given, returns a string (None if no match found).
When more than one pattern are given, returns a list of strings ([] if no match found).
"""
doc_str = parse(s)
print([param.arg_name for param in doc_str.params])
输出
['text', 'patterns']