【发布时间】:2021-08-07 09:34:30
【问题描述】:
我正在尝试找到一种方法来使用特殊标签中的希腊字符将句子括起来(在这种情况下是 LaTeX,但没关系)。所以给定我的输入文本:
inputtext = "some english text ῍Ενθεσις τοῦ Ψαλτής and then english again"
我想实现这个:
results = "some english text \textgreek{῍Ενθεσις τοῦ Ψαλτής} and then english again"
几个小时后,我想出了这个几乎可行的解决方案:
import re
inputtext = "some english text ῍Ενθεσις τοῦ Ψαλτής and then english again"
t = re.findall('[α-ωΑ-Ω]',inputtext)
beg = inputtext.find(t[0])
end = inputtext.rfind(t[-1]) + 1
results = "".join((inputtext[:beg]+"\textgreek{"+inputtext[beg:end]+"}"+inputtext[end:]))
In [50]: results
Out[50]: 'some english text ῍\textgreek{Ενθεσις τοῦ Ψαλτής} and then english again'
然后我想到了一个热门问题,有没有更好的解决方案?也许只使用正则表达式? 当前的解决方案似乎忽略了多调希腊字符῍,当然它只有在每个句子有一个希腊句子时才有效。
【问题讨论】: