英文:
Finding a string between two substrings in arabic
问题
I want to extract يذهب
.
我想提取 يذهب
。
英文:
I have a string
string = "الطالب يذهب الي الممدرسة"
I want to extract
يذهب
I have tried the following code:
import re
res = re.search('الي(.*)الطالب', string)
print(res.group(1))
But this doesn't work.
答案1
得分: 2
Switching the order of the substrings surrounding (.*)
solves the problem:
string = "الطالب يذهب الي الممدرسة"
import re
res = re.search('الي(.*)الطالب', string)
print(res.group(1))
Output: ' يذهب '
(I suspect you simply misentered the regular expression due to the way bidirectional text is displayed on your system.)
If the letters are displayed in their isolated forms, with logical ordering from left to right (that is, in the same order as the surrounding Python code), the original (faulty) string would be this:
and the corrected string would be this:
英文:
Switching the order of the substrings surrounding (.*)
solves the problem:
string = "الطالب يذهب الي الممدرسة"
import re
res = re.search('الطالب(.*)الي', string)
print(res.group(1))
Output: ' يذهب '
(I suspect you simply misentered the regular expression due to the way bidirectional text is displayed on your system.)
If the letters are displayed in their isolated forms, with logical ordering from left to right (that is, in the same order as the surrounding Python code), the original (faulty) string would be this
and the corrected string would be this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论