我想编写一个正则表达式以匹配给定输入字符串的所有子字符串。我尝试了以下方法:
def sub_string(str):
n = len(str)
# For holding all the formed substrings
output = "\\b("
# This loop maintains the starting character
for i in range(0, n):
# This loop will add a character to start character one by one till the end is reached
for j in range(i, n):
output += str[i:(j + 1)] + "|"
return output + "\\b)"
但是,它匹配错误的单词。例如,如果我输入“ h”,则表示匹配。可能是什么问题呢?还有我可以使用的另一种方法吗?
谢谢!