如何检查字符串是否包含python中带有未知字符的子字符串?

发布于 2025-01-30 16:24:54 字数 122 浏览 3 评论 0原文

抱歉,如果标题不描述,我很不擅长总结。

我想检查字符串是否包含带有未知字符的子字符串。

例如,如果我有一个像“ foobar”之类的字符串,我想检查它是否具有“ oo?ar”或“ ooba”之类的东西?

Sorry if the title is undescriptive, I'm bad at summarizing.

I want to check if a string includes a substring with an unknown character.

For example, if I have a string like "foobar", I want to check if it has something like "oo?ar" or "ooba?", signifying that a character is there, just that it can be any character.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2025-02-06 16:24:54

re.findall返回一个ROGEX(docs.python.org/3/howto/regex.html)模式的所有匹配列表。 REGEX字符匹配任何字符,但不匹配新线。

import re

txt = "foobar"

x = re.findall("oo.ar", txt) # -> ['oobar']
y = re.findall("ooba.", txt) # -> ['oobar']
print(x)
print(y)

re.findall returns a list of all matches of a regex (docs.python.org/3/howto/regex.html) pattern in a string. The regex character . matches any character, but not a newline.

import re

txt = "foobar"

x = re.findall("oo.ar", txt) # -> ['oobar']
y = re.findall("ooba.", txt) # -> ['oobar']
print(x)
print(y)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文