使用 Python 正则表达式判断字符串是否包含斜杠 '/'

可以使用以下正则表达式来判断某个字符串是否包含 '/':

import re

string = 'hello/world'
pattern = r'/'

if re.search(pattern, string):
    print('包含/')
else:
    print('不包含/')

输出:

包含/

解释:

  • re.search(pattern, string) 函数用于在字符串 string 中搜索匹配正则表达式 pattern 的第一个位置。
  • r'/' 表示正则表达式,其中 / 表示要匹配的字符。

总结:

使用 Python 的 re 库可以方便地判断字符串是否包含特定字符,例如斜杠 '/'。通过正则表达式匹配,可以轻松实现各种字符串处理操作。

Python 正则表达式判断字符串是否包含斜杠 '/'

原文地址: https://www.cveoy.top/t/topic/nlLy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录