Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.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 2.7正则表达式语句,用于向第一个匹配字符添加锐重音_Python_Regex_Python 2.7 - Fatal编程技术网

Python 2.7正则表达式语句,用于向第一个匹配字符添加锐重音

Python 2.7正则表达式语句,用于向第一个匹配字符添加锐重音,python,regex,python-2.7,Python,Regex,Python 2.7,我试图使用Python正则表达式为两个字符模式的第一个匹配字符添加一个尖锐的重音。例如,我希望oä́变成oä́。下面是我正在使用的正则表达式代码 raw = oä́gtra’ raw = re.sub(ur"([ieäaouëö])([í|é|ä́|á|ó|ú|ö́|ë́])", ur"\1́\2", raw) # notice the acute accent between \1 and \2 为了帮助解决这个问题,我使用以下语句 if re.match(ur"([ieäaouëö])(

我试图使用Python正则表达式为两个字符模式的第一个匹配字符添加一个尖锐的重音。例如,我希望
oä́
变成
oä́
。下面是我正在使用的正则表达式代码

raw = oä́gtra’

raw = re.sub(ur"([ieäaouëö])([í|é|ä́|á|ó|ú|ö́|ë́])", ur"\1́\2", raw) # notice the acute accent between \1 and \2
为了帮助解决这个问题,我使用以下语句

if re.match(ur"([ieäaouëö])([í|é|ä́|á|ó|ú|ö́|ë́])", raw) is not None:
    print "it found the pattern..."
语句“it found The pattern…”被打印出来,因此我的regex语句似乎正确地识别了模式。我只需要帮助将尖锐重音添加到第一个匹配字符中

下面是我也尝试过的其他代码。但是这个代码似乎也不起作用

print repr(raw) # prints u'o\xe4\u0301gtra\u2019'
mapping = {"i":"í","e":"é","ä":"ä́","a":"á","o":"ó","u":"ú","ö":"ö́","ë":"ë́"}
pattern = "([ieäaouëö])([í|é|ä́|á|ó|ú|ö́|ë́])"
replacement = lambda match: mapping[match.group(1)] + match.group(2)
raw = re.sub(pattern, replacement, raw)

感谢您提供的所有回复和今后的任何帮助!非常感谢

您可以尝试使用
ord
chr
和编码来实现一些神奇的功能,但我认为您最好对映射进行硬编码

mapping = {"i":"í","e":"é","ä":"ä","a":"á","o":"ó","u":"ú","ö":"ö","ë":"ë"}
pattern = "([aeiou])([aeiou])"
replacement = lambda match: mapping[match.group(1)] + match.group(2)
text = re.sub(pattern, replacement, text)

请注意,它是不完整的,您需要扩展字典,并且正则表达式模式

不是无的
是冗余的。在这种情况下,如果存在匹配项,它将在用作布尔值时自动计算为True。答案取决于您如何表示重音:组合或分解。例如,
i
可以是
\xed
i\u0301
repr(raw)
返回什么?感谢您的帮助,请回答更新后的问题。如果您在Python 2源代码中有非ascii字符串,请务必回答。感谢您的帮助,我尝试了我认为正在扩展字典和正则表达式模式的方法,但我似乎仍然无法使其正常工作。