import re
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groupdict())
此代码段生成输出
{'形容词':'快','动词':'是','动物':'马'}
两个问题。
一个模式如何匹配句子?匹配的对象还包含用于创建它的模式吗?
答案 0 :(得分:0)
不,正则表达式只是定义搜索模式。对于词性标注,您需要更强大的功能,即NLTK library。
/opt/web/env/lib/python3.5/site-packages/django/urls/resolvers.py in url_patterns
iter(patterns)
Local Vars
Variable Value
msg ("The included URLconf '{name}' does not appear to have any patterns in it. If "
'you see valid patterns in the file then the issue is probably caused by a '
'circular import.')
patterns <module 'app_name.urls' from '/opt/web/app/my_site/app_name/urls.py'>
self <URLResolver 'app_name.urls' (None:None) '^/'>
During handling of the above exception ('module' object is not iterable), another exception occurred:
/opt/web/env/lib/python3.5/site-packages/django/core/handlers/exception.py in inner
response = get_response(request)
[SNIP]
/opt/web/env/lib/python3.5/site-packages/django/urls/base.py in resolve
return get_resolver(urlconf).resolve(path)
Local Vars
Variable Value
path '/4567/details'
urlconf 'app_name.urls'
/opt/web/env/lib/python3.5/site-packages/django/urls/resolvers.py in resolve
for pattern in self.url_patterns:
Local Vars
Variable Value
args ()
kwargs {}
match ('4567/details', (), {})
new_path '4567/details'
path '/4567/details'
self <URLResolver 'app_name.urls' (None:None) '^/'>
tried []
/opt/web/env/lib/python3.5/site-packages/django/utils/functional.py in __get__
res = instance.__dict__[self.name] = self.func(instance)
Local Vars
Variable Value
cls <class 'django.urls.resolvers.URLResolver'>
instance <URLResolver 'app_name.urls' (None:None) '^/'>
self <django.utils.functional.cached_property object at 0x7f06c7d2c550>
/opt/web/env/lib/python3.5/site-packages/django/urls/resolvers.py in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
Local Vars
Variable Value
msg ("The included URLconf '{name}' does not appear to have any patterns in it. If "
'you see valid patterns in the file then the issue is probably caused by a '
'circular import.')
patterns <module 'app_name.urls' from '/opt/web/app/my_site/app_name/urls.py'>
self <URLResolver 'app_name.urls' (None:None) '^/'>
所有这一行都按顺序捕获每个组,然后命名。 See here以及标题为&#34;非捕获和命名群组的下一部分&#34;