Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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/9/apache-flex/4.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
如何使用python正则表达式sub_Python_Regex_String_Python 3.x_Pattern Matching - Fatal编程技术网

如何使用python正则表达式sub

如何使用python正则表达式sub,python,regex,string,python-3.x,pattern-matching,Python,Regex,String,Python 3.x,Pattern Matching,我正在尝试替换以下字符串 word = 'hello contractor salary hour year' 有图案的 pattern = r'\b(contrator|per|salary|year|hour)' word = re.sub(pattern, '', word, re.I) 但并不是所有的单词都被替换 我得到的结果是“hello hour year” 你知道我怎么修吗 感谢Kirill Bulygin的回答,这是一个常见的问题,请使用flags=(re.sub(patt

我正在尝试替换以下字符串

word = 'hello contractor salary hour year' 
有图案的

pattern = r'\b(contrator|per|salary|year|hour)'
word = re.sub(pattern, '', word, re.I)
但并不是所有的单词都被替换 我得到的结果是“hello hour year” 你知道我怎么修吗


感谢Kirill Bulygin的回答,这是一个常见的问题,请使用
flags=
re.sub(pattern',word,flags=re.I)
)。否则,由
re.I
指定的人工数字将用作
count

谢谢,它可以工作,但您介意解释一下原因吗?是的,它很好,但如果我添加一些其他规则,如(…|月(ly)?|…),它将再次失败,你知道为什么吗?从你提供的信息中我只能看出它与
re.I
count
无关。我建议创建一个新问题,包括您的代码、输入、实际输出和所需输出。谢谢…我忘记传递标志=。。。很抱歉。