2024-05-12 11:50:35 发布
网友
我目前正在使用pyparsing来识别字符串中是否使用了嵌套括号,以便识别错误地连接到单词的引用号。你知道吗
例如,“苹果(4)”。你知道吗
我希望能够识别引用子项('(4)')。但是,当我使用searchString时,它返回一个ParseResults对象[[7]],它不提供括号。我想在原始标记中找到子字符串,因此需要在ParseResults对象中包含嵌套字符。Ie,我要搜索'(4)'。有没有办法让searchString返回嵌套字符。你知道吗
Question: Is there a way to make searchString return the nest characters.
考虑以下示例:
data = 'apple(4), banana(13), juice(1)'
from pyparsing import Word, nums, alphas nested = Word(alphas) + '(' + Word(nums) + ')' for item in data.split((',')): print(item, "->", nested.searchString(item))
Output:apple(4), ->[['apple', '(', '4', ')']] banana(13), ->[['banana', '(', '13', ')']] juice(1), ->[['juice', '(', '1', ')']]
Output:
apple(4), ->[['apple', '(', '4', ')']] banana(13), ->[['banana', '(', '13', ')']] juice(1), ->[['juice', '(', '1', ')']]
import re nObj = re.compile('(\w+?)(\(\d+\))') findall = nObj.findall(data) print('findall:{}'.format(findall))
Output: findall:[('apple', '(4)'), ('banana', '(13)'), ('juice', '(1)')]
findall:[('apple', '(4)'), ('banana', '(13)'), ('juice', '(1)')]
用Python:3.4.2测试
考虑以下示例:
用Python:3.4.2测试
相关问题 更多 >
编程相关推荐