Python re.sub更改被替换的波斯语/阿拉伯语内容的方向
这是我的密码: 进口稀土 自定义的\u子\u模式=\{{{}:\s*{tag\u key}?:\s*\\\\?:\s*[^{}]+?:\s*\} pattern=re.compileCUSTOMIZED\u SUB\u pattern.formattag\u key='name' title=عزیز{name |默认值1}}سلام 关于子模式,标题 输出: 但我想要的是: 因此,正如你所看到的,句子的方向随着替换而改变 问题:Python re.sub更改被替换的波斯语/阿拉伯语内容的方向,python,regex,arabic,re,farsi,Python,Regex,Arabic,Re,Farsi,这是我的密码: 进口稀土 自定义的\u子\u模式=\{{{}:\s*{tag\u key}?:\s*\\\\?:\s*[^{}]+?:\s*\} pattern=re.compileCUSTOMIZED\u SUB\u pattern.formattag\u key='name' title=عزیز{name |默认值1}}سلام 关于子模式,标题 输出: 但我想要的是: 因此,正如你所看到的,句子的方向随着替换而改变 问题: 如何解决此问题?此答案不正确 >>> x =
如何解决此问题?此答案不正确
>>> x = 'walk down street'
>>> x.split(' ')
['walk', 'down', 'street']
>>> x.split(' ')[::-1]
['street', 'down', 'walk']
>>> ' '.join(x.split(' ')[::-1])
'street down walk'
希望我能帮忙 通过此模块,您可以纠正文本形状的方向。只需安装PIP并使用它
# install: pip install --upgrade arabic-reshaper
import arabic_reshaper
# install: pip install python-bidi
from bidi.algorithm import get_display
text = "ذهب الطالب الى المدرسة"
reshaped_text = arabic_reshaper.reshape(text) # correct its shape
bidi_text = get_display(reshaped_text) # correct its direction
可以使用和库来相应地重塑和替换RTL文本
get_显示方法中有一个特殊选项,它是base_dir,具有“L”或“R”,覆盖计算的base_级别
你可以尝试:
import re
import arabic_reshaper
from bidi.algorithm import get_display
title = "عزیز {{ name | defalue value 1}} سلام"
substr = "محمد"
reshaped_text = arabic_reshaper.reshape(title)
new_title = get_display(reshaped_text, base_dir = 'L') # 'L' option indicates the text to appear from Left to Right. By default; it is RTL for Arabic texts.
reshaped_text2 = arabic_reshaper.reshape(substr)
new_substr = get_display(reshaped_text2, base_dir = 'L')
CUSTOMIZED_SUB_PATTERN = "\{{\{{(?:\s)*{tag_key}(?:\s)*\|(?:\s)*([^|}}]+)(?:\s)*\}}\}}"
pattern = re.compile(CUSTOMIZED_SUB_PATTERN.format(tag_key='name'))
print(re.sub(pattern, new_substr, new_title))
您可以在中找到上述实现的示例运行结果,它将不起作用,因为这是一个方向错误,它将把事情搞得更糟。Reformated_text=arabic_Reformater.Reformatere.subpattern,محمد,title和get_display Reformated_text引导我到'ﻡﻼﺳ ﺪﻤﺤﻣ ﺰﯾﺰﻋ'嗨@DeltaHaxor;当你提供答案时,请在问题中添加所需的内容。请至少提供一个运行结果示例。这可能有助于OP.Try使用PyPi正则表达式模块。它在字符串开头设置从左到右的覆盖,并用\u202B/\u202C标记包装每个阿拉伯单词,确保阿拉伯单词从右到左的顺序。@revo感谢您的回复。你知道这是我们通常从右到左写句子的方式,这是人们理解我在语言中所写内容的方式。如果我按照你的建议修改我的句子,那将不是一个有意义的句子。@Mandy8055我感谢你的回答。非常感谢。@Mandy8055是的,绝对是。@Mohammasmasoumi salam refigh,mokhelsam。我所说的是,我所说的是,如果你首先键入你的句子,没有占位符,没有占位符,你的句子是没有占位符的句子,你的句子是没有占位符的句子,你的句子是没有占位符的句子,如果你第一次键入你的句子是没有占位符的句子,你的句子是没有占位符的句子,如是,即,即,即,即,你的替换字符串将是你的替换字符串替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的替换字符串将是你的全名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名{name | deflaue value 1}}سلام。我希望你现在明白我的意思。
# install: pip install --upgrade arabic-reshaper
import arabic_reshaper
# install: pip install python-bidi
from bidi.algorithm import get_display
text = "ذهب الطالب الى المدرسة"
reshaped_text = arabic_reshaper.reshape(text) # correct its shape
bidi_text = get_display(reshaped_text) # correct its direction
import re
import arabic_reshaper
from bidi.algorithm import get_display
title = "عزیز {{ name | defalue value 1}} سلام"
substr = "محمد"
reshaped_text = arabic_reshaper.reshape(title)
new_title = get_display(reshaped_text, base_dir = 'L') # 'L' option indicates the text to appear from Left to Right. By default; it is RTL for Arabic texts.
reshaped_text2 = arabic_reshaper.reshape(substr)
new_substr = get_display(reshaped_text2, base_dir = 'L')
CUSTOMIZED_SUB_PATTERN = "\{{\{{(?:\s)*{tag_key}(?:\s)*\|(?:\s)*([^|}}]+)(?:\s)*\}}\}}"
pattern = re.compile(CUSTOMIZED_SUB_PATTERN.format(tag_key='name'))
print(re.sub(pattern, new_substr, new_title))