python - 查找字符串中第一个数字的索引

标签 python regex string

我有一个类似

的字符串
"xdtwkeltjwlkejt7wthwk89lk"

如何获取字符串中第一个数字的索引?

最佳答案

使用 re.search() :

>>> import re
>>> s1 = "thishasadigit4here"
>>> m = re.search(r"\d", s1)
>>> if m:
...     print("Digit found at position", m.start())
... else:
...     print("No digit in that string")
... 
Digit found at position 13

关于python - 查找字符串中第一个数字的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4510709/

相关文章:

python - gunicorn 与 gevent worker : Using a shared global list

javascript - 在 js 文件中找不到 Django、Ajax url

asp.net - 正则表达式只允许特定数字

regex - mod_rewrite regex 仅在某个字符串不存在时才匹配

C 编程 - 操作字符串数组时出现段错误

Java字符串格式化问题

Java split() 一个由您要拆分的字符串组成的字符串?

python - plt.plot()、object.plot() 之间有什么区别

regex - 替换具有多个字符的字符串

python - 将字符串编译为 Ruby 字节码以获得更好的性能——就像 Python 中的 compile()