当我执行' m.group()' ,如果图案匹配,则打印图案而不是与其匹配的单词。有人可以帮忙吗?
import sys
import re
def main():
str = sys.argv[1]
m = re.search(str,'this is a sample text')
print m.group()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
您可以使用re.findall代替:
<强> re.py:强>
import sys
import re
def main():
str = sys.argv[1]
for word in re.findall(r"\w+", 'this is a sample text'):
if str in word:
print word
if __name__ == '__main__':
main()
使用示例:
C:\Users\USER\Google Drive\Python_stuff>re.py tex
text