我正在尝试编写一个正则表达式,用于提取具有以下属性的字符串:
我尝试了以下正则表达式,但它不适合我
r'[A-Z](\w|\.)*(\s(([A-Z](\w|\.)*)|&))*
答案 0 :(得分:2)
正则表达式是匹配模式。您可以使用动态编程来解决这个最长的子字符串问题。
string = raw_input()
longest_substring = [0]
for i in range(len(string)):
pre_longest = longest_substring[-1]
if pre_longest == 0:
if string[i].isupper() or string[i] == '&':
cur_longest = 1
else:
cur_longest = 0
else:
if i > 0 and string[i-1] == '&':
cur_longest = 0
elif string[i].isalpha() or string[i] == '.':
cur_longest = pre_longest + 1
else:
cur_longest = 0
longest_substring.append(cur_longest)
print longest_substring