作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。
想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。
8 年前关闭。
Improve this question
我从 python 脚本中的命令行参数中获取了一些字符串,并希望将这些字符串用作正则表达式。怎么做?
最佳答案
此代码段应该可以帮助您了解要执行的操作。 (Python 2.7.5)
>>> import re
>>> user_string = raw_input('please enter a string to convert to regex: ')
please enter a string to convert to regex: ab(c)
>>> regex = re.compile(user_string)
>>> regex.match('abc').groups()
('c',)
raw_input
获取输入然后
re.compile
将其转换为正则表达式,然后您可以随意使用它。
raw_input
与
input
关于python - 如何在python中将字符串转换为正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19977108/
我是一名优秀的程序员,十分优秀!