Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我无法在matchobject(regex python)中打印匹配的完整单词_Regex_Python 2.7 - Fatal编程技术网

我无法在matchobject(regex python)中打印匹配的完整单词

我无法在matchobject(regex python)中打印匹配的完整单词,regex,python-2.7,Regex,Python 2.7,当我执行'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() 您可以这样使用: re.py: import sys import re def main()

当我执行'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()

您可以这样使用:

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
用法示例:

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
您可以这样使用:

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
用法示例:

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

输出:C:\Users\USER\Google Drive\Python\u stuff>re.py tex您是否要打印包含图案的字?使用
str=r'\b\w*'+re.escape(sys.argv[1])+r'\w*'
OUTPUT:C:\Users\USER\Google Drive\Python\u stuff>re.py tex您是否试图打印包含图案的字?使用
str=r'\b\w*'+re.escape(sys.argv[1])+r'\w*'