sách gpt4 ai đã đi

python简单路由器url匹配器,如何重新匹配第一个 "/"出现

In lại 作者:太空宇宙 更新时间:2023-11-03 18:48:47 29 4
mua khóa gpt4 Nike

我有一个路由器模块,它将主题与正则表达式进行比较,并将出现的事件与一致的键掩码链接起来。 (它是一个简单的 url 路由过滤,如 symfony http://symfony.com/doc/current/book/routing.html )

import re
from functools import partial

def to_named_groups(match, regexes):
group_name = re.escape(match.group(0)[1:-1])
group_regex = regexes.get(group_name, '.*')
return '(?P<{}>{})'.format(group_name, group_regex)

def make_regex(key_mask, regexes):
regex = re.sub(r'\{[^}]+\}', partial(to_named_groups, regexes=regexes),
key_mask)
return re.compile(regex)

def find_matches(key_mask, text, regexes=None):
if regexes is None:
regexes = {}
thử:
return make_regex(key_mask, regexes).search(text).groupdict()
except AttributeError:
return None

.

<小时>
find_matches('foo/{one}/bar/{two}/hello/{world}', 'foo/test/bar/something/hello/xxx')

Đầu ra:

{'one': 'test', 'two': 'something', 'world': 'xxx'} Blockquote

<小时>
find_matches('hello/{city}/{phone}/world', 'hello/mycity/12345678/world', regexes={'phone': '\d+'})

Đầu ra:

{'city': 'mycity', 'phone': '12345678'} Blockquote

<小时>
find_matches('hello/{city}/{phone}/world', 'hello/something/mycity/12345678/world', regexes={'phone': '\d+'})

Đầu ra:

{'city': 'something/mycity', 'phone': '12345678'}

这是不匹配的(应该返回 None 而不是 'city': 'something/mycity')。我该如何解决这个问题?我如何匹配第一个“/”出现或其他方式?

Cảm ơn!

1 Câu trả lời

让我们看看您正在构建的正则表达式:

hello/(?P.*)/(?P\d+)/world

只要剩下足够的斜杠来匹配模式的其余部分,.* 将匹配任何内容,包括其中带有斜杠的内容。

如果您不希望它匹配斜杠...您已经知道该怎么做,因为您在 re.sub 中执行完全相同的操作:使用所有内容的字符类但是斜线,而不是点。

def to_named_groups(match, regexes):
group_name = re.escape(match.group(0)[1:-1])
group_regex = regexes.get(group_name, '[^/]*')
return '(?P<{}>{})'.format(group_name, group_regex)
<小时>

但与此同时,如果您不理解正在构建的正则表达式,那么为什么要构建它们?您只需使用 .split('/') 就可以更轻松地将其解析为路径分隔的组件。例如,如果没有额外的正则表达式,我认为这就是您想要的:

def find_matches(key_mask, text):
mapping = {}
for key, value in zip(key_mask.split('/'), text.split('/')):
if key[0] == '{' and key[-1] == '}':
mapping[key[1:-1]] = value
elif key != value:
trở lại
return mapping

正则表达式只是添加一些验证检查的一种方法。 (正如所写,它可以用来破坏正常的斜杠分隔方案,但我认为这是一个错误,而不是一个功能 - 事实上,我认为这正是促使您首先进入 StackOverflow 的错误。)所以,只需明确地执行即可:

def find_matches(key_mask, text, regexes={}):
mapping = {}
for key, value in zip(key_mask.split('/'), text.split('/')):
if key[0] == '{' and key[-1] == '}':
key=key[1:-1]
if key in regexes and not re.match(regexes[key], value):
trở lại
mapping[key] = value
elif key != value:
trở lại
return mapping
<小时>

第二个版本已经阻止正则表达式匹配 /,因为您在应用斜杠之前就已经将它们分开了。因此,您不需要在评论中要求进行清理。

但无论哪种方式,清理正则表达式的最简单方法是在使用它们之前对其进行清理,而不是使用正则表达式将所有内容构建到一个大正则表达式中,然后尝试对其进行清理。例如:

regexes = {key: regex.replace('.*', '[^/]*') for key, regex in regexes.items()}

关于python简单路由器url匹配器,如何重新匹配第一个 "/"出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18860759/

29 4 0
Bài viết được đề xuất: mysql - "AS"clsql中的伪运算符语法
Bài viết được đề xuất: lisp - 如何在 Common Lisp 中加载库?
Bài viết được đề xuất: c# - 将url参数字符串转换为路径的正则表达式
Bài viết được đề xuất: LISP - 将语法输入转换为字符串(语言理论)
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com