python - 简单的字符串匹配

标签 python string-matching

我想做一个简单的字符串匹配,从字符串的 -front- 开始搜索。有没有更简单的方法可以使用内置函数来做到这一点? (re 似乎矫枉过正)

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'):
    if ipadr[0:len(matchIP)] == matchIP: return True
    return False

最佳答案

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'):
    return ipadr.startswith(matchIP)

关于python - 简单的字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5969465/

相关文章:

python - python 中的字符串替换 - % : 'list' and 'str' 不支持的操作数类型

python - 如何访问 python LogRecord 的 asctime?

python - 检查 pandas 字符串列是否包含多个单词(按任意顺序)

Mysql 'match against' 具有多个条件

python - 如何将对象更改为数字?

python - Mac OS X 与 Linux 上的 Numpy 和内存分配

Python IndentationError - 如何重构?

regex - 匹配 float >= 100

java - 查看文件是否读取了 Java 中的内容

python - 如何根据多个条件提取子串?