当原始字符串使用特殊字符的组合,例如不等式(< =&> =)时,如何将模式传递给compile()方法?

发布于 2025-02-03 21:39:31 字数 547 浏览 1 评论 0原文

我如何获取我的re.compile函数以识别结合特殊字符(> =&< =)的不平等现象?它只能拿起相等的标志,我需要两个符号。

代码:

constraint_re = re.compile(r"(?P<prefix>['~','^','>=','<='])?(?P<major>\d+)(\.(?P<minor>(\d+|x)))?(\.(?P<patch>(\d+|x)))?(-.*)?")

testmatch = constraint_re.search("~0.0.2")
testmatch.group("prefix")

输出:

'~'

的测试代码

testmatch_1 = constraint_re.search("<=0.0.2")
testmatch_1.group("prefix")

&lt; =输出

'='

How do I get my re.compile function to identify inequalities that combine special characters ( >= & <=) ? It only picks up the equal sign and I need both symbols together.

Code:

constraint_re = re.compile(r"(?P<prefix>['~','^','>=','<='])?(?P<major>\d+)(\.(?P<minor>(\d+|x)))?(\.(?P<patch>(\d+|x)))?(-.*)?")

testmatch = constraint_re.search("~0.0.2")
testmatch.group("prefix")

Output:

'~'

Test code for <=

testmatch_1 = constraint_re.search("<=0.0.2")
testmatch_1.group("prefix")

Output

'='

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文