Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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/2/spring/13.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
Regex 如何使用正则表达式替换带有问号的句子_Regex_Python 3.x - Fatal编程技术网

Regex 如何使用正则表达式替换带有问号的句子

Regex 如何使用正则表达式替换带有问号的句子,regex,python-3.x,Regex,Python 3.x,我正试图写一些代码将莎士比亚的英语翻译成现代英语,但我在使用re.sub替换以问号结尾的句子时遇到了问题。当我替换句子时,我在结尾处得到2个问号 我已经尝试将“\?”和“\?”添加到替换字符串中,但这不起作用 p = 'Mine eye hath played the painter and hath stelled. Thy beauty form in table of my heart; My body is the frame wherein this held, and perspec

我正试图写一些代码将莎士比亚的英语翻译成现代英语,但我在使用re.sub替换以问号结尾的句子时遇到了问题。当我替换句子时,我在结尾处得到2个问号

我已经尝试将“\?”和“\?”添加到替换字符串中,但这不起作用

p = 'Mine eye hath played the painter and hath stelled. Thy beauty form in table of my heart; My body is the frame wherein this held, and perspective it is the painters art. For through the painter must you see his skill, to find where your true image pictured lies; Which in my bosoms shop is hanging still, that hath his windows glazed with thine eyes. Now see what good turns eyes for eyes have done. Have mine eyes have drawn thy shape, and thine for me? Are windows to my breast, where-through the sun delights to peep, to gaze therein on thee; Yet eyes this cunning want to grace their art; They draw but what they see, know not the heart.'
s = 'Have mine eyes have drawn thy shape, and thine for me?'
s2 = re.sub(r'^(.*?(mine.*?){0})mine', r'\1my', s)
p = re.sub(s,s2,p)
print(p)
下面是输出,正如您将注意到的,在我替换的句子末尾有两个问号

Mine eye hath played the painter and hath stelled. Thy beauty form in table of my heart; My body is the frame wherein this held, and perspective it is the painters art. For through the painter must you see his skill, to find where your true image pictured lies; Which in my bosoms shop is hanging still, that hath his windows glazed with thine eyes. Now see what good turns eyes for eyes have done. Have my eyes have drawn thy shape, and thine for me?? Are windows to my breast, where-through the sun delights to peep, to gaze therein on thee; Yet eyes this cunning want to grace their art; They draw but what they see, know not the heart.

s
字符串在
re.sub(s,…)
中用作正则表达式,因此如果尚未转义特殊字符,则必须转义

在这种情况下,如果不这样做,将告诉regex模块搜索0或1
e
字符。以下
在输入字符串中不匹配,这说明您得到的是旧的
和新的

在这里,我要做:

p = re.sub(re.escape(s),s2,p)
(这比:
p=re.sub(s.replace(“?”,r“\?”),s2,p)更通用