Python正则表达式在向前的方向上查找给定字符串中的“document”单词,并替换为空字符串
如果word文档可以通过从给定字符串中删除字符来生成,则将从字符串中删除拼写文档的字母。如果可以删除结果字符串中的字母以离开字符串文档,则该字符串中的字母拼写文档将被删除。这种情况一直持续到无法删除字母以离开文档为止,此时将返回最终字符串 例如,如果字符串为:Python正则表达式在向前的方向上查找给定字符串中的“document”单词,并替换为空字符串,python,regex,Python,Regex,如果word文档可以通过从给定字符串中删除字符来生成,则将从字符串中删除拼写文档的字母。如果可以删除结果字符串中的字母以离开字符串文档,则该字符串中的字母拼写文档将被删除。这种情况一直持续到无法删除字母以离开文档为止,此时将返回最终字符串 例如,如果字符串为: documdocumententer ^^^^^^^^ adbocucdmefgnhtj ^ ^^^ ^^ ^ ^ 文档可以从一开始就删除DOCUM并在结尾处输入,所以中间的文档被删除,留下 documenter ^^
documdocumententer
^^^^^^^^
adbocucdmefgnhtj
^ ^^^ ^^ ^ ^
文档可以从一开始就删除DOCUM并在结尾处输入,所以中间的文档被删除,留下
documenter
^^^^^^^^
然后重复该过程以离开
er
由于er不包含文档,因此将返回er
类似地,如果字符串为:
documdocumententer
^^^^^^^^
adbocucdmefgnhtj
^ ^^^ ^^ ^ ^
将删除字母拼写文档以留下:
abcdfghj
将返回此字符串,因为它不包含文档
例子
doconeument已转换为一
documdocumentent已转换为空字符串
文档一被转换为一
pydocdbument被转换为pydb
documentdocument已转换为空字符串
如何仅从特定word文档的给定字符串中获取感兴趣的字符串
我用python for loop尝试了这个查询,但我不知道如何只使用正则表达式
我的代码在下面
import re
def fun1(text):
print('original string:', text)
pattern = r"((d|D).*o.*c.*u.*m.*e.*n.*t){1,}"
result = re.sub(pattern, '', text)
if len(result) == len(text):
print('return original string because it does not contain "document" word forward direction:')
return text
# if word is containing "document" in forward direction
temp = [] # for storing letter and its index
# find each letter and index in "document" word
search_str = 'document'
for index in range(len(search_str)):
# if it is a last letter in "document" that is t
if index == len(search_str)-1:
current_letter = search_str[index]
pattern = r'.*n.*t'
else:
next_letter = search_str[index + 1]
current_letter = search_str[index]
pattern = rf".*{current_letter}.*{next_letter}"
result = re.match(pattern, text)
a, b = result.span()
if temp:
# value of last dict in temp list
val = list(temp[-1].values())[0]
current_letter = val + text[val:].index(current_letter)
else:
# first time when temp list is empty
current_letter = text[a:b].rindex(current_letter)
temp.append({search_str[index]: current_letter})
# now using temp list we remove "document" word at specific index
text = list(text)
# create a list with index decending order to remove from text
remove_index_list = [list(i.values())[0] for i in temp]
remove_index_list.sort(reverse=True)
for j in remove_index_list:
text.pop(j)
final_txt = ''.join(text)
# to check if text containing or not one more "document" word
pattern = r"((d|D).*o.*c.*u.*m.*e.*n.*t){1,}"
result = re.findall(pattern, final_txt)
if result:
print('The word again containing "document" in it')
final_txt = fun1(final_txt)
return final_txt
print('final_output:', fun1('doconeument'))
我有一个正则表达式和递归的解决方案:
from re import compile
candidates = ["doconeument", "documdocumentent", "documentone",
"pydocdbument", "documentdocument", "hansi"]
word = "document"
def strip_word(word, candidate):
regex = compile("^(.*)" + "(.*)".join(word) + "(.*)$")
match = regex.match(candidate)
if not match:
return candidate
return strip_word(word, "".join(match.groups()))
for cand in candidates:
print(f"'{cand}' -> '{strip_word(word, cand)}'")
编辑:对代码进行了更正。函数的前两行保留在外。如果给定字符串与正则表达式不匹配:
r'^([a-z]*)d([a-z]*)o([a-z]*)c([a-z]*)u([a-z]*)m([a-z]*)e([a-z]*)n([a-z]*)t([a-z]*)$'
返回字符串。如果正则表达式与字符串匹配,则字符串:
"\1\2\3\4\5\6\7\8\9"
并尝试将该字符串与正则表达式匹配。重复此过程,直到没有匹配为止,此时返回最后一个测试的字符串。请注意,由此产生的每个字符串比前一个字符串少8个字符
如果正则表达式与字符串匹配,则捕获组1将包含文档中d之前的子字符串,捕获组2将包含d和o之间的子字符串,依此类推,捕获组9包含t之后的子字符串。这些子字符串中的部分或全部可能为空
我将让OP来生成实现此算法所需的Python代码。数据文档-da或ad的输出应该是什么?@DavidWierichs请注意我的编辑。。。没有它,这个词就被硬编码了。现在它应该适用于可变词。我对此感到非常困惑,如果可以的话,我会给它多投一票。Wow.thanx for ans并完美地描述我的问题