请检查要求这不是重复的问题。 需要找出使用Regx
的第一次非重复连续出现的charecterStr1="aabbcdde"
output:- c
str2="abbccddee"
Output:- a
答案 0 :(得分:1)
您可以使用itertools.groupby
将字符组合在一起,然后计算这些组的长度,当您找到长度为1时停止。
from itertools import groupby
def non_repeating(s):
for k, g in groupby(s):
if sum(1 for _ in g) == 1: # This can probably be improved
return k
return None # Or whatever failure value is appropriate
编辑:
这是一个函数,它接受一些迭代器并测试它是否是一个元素长或不占用它的一部分
from itertools import groupby
def is_one(g):
try:
next(g) # Has a first element, should succeed
except StopIteration:
return False # Empty iterator
try:
next(g) # Has a second element should fail
except StopIteration:
return True # There was only one element
return False # There was more than one element
def non_repeating(s):
for k, g in groupby(s):
if is_one(g):
return k
return None # Or whatever failure value is appropriate
答案 1 :(得分:0)
我没有用python编写代码,但是我知道此正则表达式可以捕获repeated
的任何字符匹配。如果您只想捕获.
\w
调整为word
(\w)\1+
this regex means: match all characters repeated
您可以将所有匹配项替换为空字符串,然后字符串的第一个字符是第一个non-repeated
字
答案 2 :(得分:0)
某些re
解决方案-只是查找重复的字符,而不是查找未重复的字符。
import re
str1="aabbcdde"
str2="abbccddee"
print(str1)
for el in re.sub(r"(.)\1+", "", str1):
print(el)
print(str2)
for el in re.sub(r"(.)\1+", "", str2):
print(el)
输出:
aabbcdde
c
e
abbccddee
a
答案 3 :(得分:-1)
以下正则表达式可用于删除重复的字母。
preg_replace("/(?<first>[a-z]+)(\k<first>)/", "", $input_lines);
然后你可以拔出第一个字母(因为你的第一个例子有两个非重复的字母)。在enter link description here
上测试过