我需要一个正则表达式来删除字符串中的重复字符,如下所示:
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.BUILD-SNAPSHOT")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'empresalia'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/release" }
maven { url "http://repo.spring.io/milestone" }
maven { url "http://repo.spring.io/snapshot" }
}
configurations {
providedRuntime
}
mainClassName = "universando.Application"
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.3.0.BUILD-SNAPSHOT")
compile("org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE")
compile("org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE")
compile("org.springframework:spring-context-support:4.1.6.RELEASE")
compile("org.springframework:spring-context:4.1.6.RELEASE")
compile("org.springframework:spring-jdbc:4.1.6.RELEASE")
compile("org.springframework:spring-beans:4.1.6.RELEASE")
compile("org.springframework:spring-tx:4.1.6.RELEASE")
compile("org.springframework.security:spring-security-web:3.2.7.RELEASE")
compile("org.springframework.security:spring-security-config:3.2.7.RELEASE")
compile("org.springframework.security:spring-security-core:3.2.7.RELEASE")
compile("org.springframework.security:spring-security-test:4.0.1.RELEASE")
compile("javax.mail:mail:1.4.7")
compile("javax.activation:activation:1.1")
compile("org.apache.velocity:velocity:1.7")
compile("org.apache.velocity:velocity-tools:2.0")
compile("com.jayway.jsonpath:json-path:0.9.1")
compile("com.maxmind.geoip:geoip-api:1.2.10")
compile("com.cybozu.labs:langdetect:1.1-20120112")
compile("org.quartz-scheduler:quartz:2.2.1")
compile("mysql:mysql-connector-java:5.1.6")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat:1.2.3.RELEASE")
testCompile("org.springframework.boot:spring-boot-starter-test:1.2.3.RELEASE")
}
task wrapper(type: Wrapper) { gradleVersion = '1.10' }
至abcdeafghid
,删除bcefghi
和a
我不知道如何诚实地对待这件事。我可以找到很多关于删除重复项的内容,但它们总是留下一个重复字符的实例。
最后字符的顺序无关紧要,但由于我正在使用CJK语言,它应该支持这些。我该怎么做呢?
答案 0 :(得分:2)
您可以采取以下两种方式之一:
Python中的一行:
>>> s='abcdeafghid'
>>> ''.join(c for c in s if s.count(c)==1) # only keep the ones that are singular
'bcefghi'
或者,过滤掉有多个的那些:
>>> s='abcdeafghid'
>>> filter(lambda c: s.count(c)==1, s)
'bcefghi'
在Perl中,您将使用字符串中每个字符的计数创建一个哈希,然后使用正则表达式删除该字符,或者如果计数等于1则添加:
my $s='abcdeafghid';
my %h;
$h{$_}++ foreach (split //, $s); # count of all characters
while(my($k, $v) = each %h){ $s =~ s/$k//g if $v>1}
print $s;
或者,
my $s='abcdeafghid';
my %h;
my $new='';
$h{$_}++ foreach (split //, $s);
foreach (split //, $s) { $new.=$_ if $h{$_}==1}
print $new;
答案 1 :(得分:1)
与您的语言无关,您可以使用以下伪代码:
Dictionary dict
for i = 0 to Len(your_string)
if Not(dict.Exits(your_string[i])) then
dict.Add(your_string[i],1)
else
dict[your_string[i]] += 1
end if
Next i
int index = 0
while 1
if dict[your_string[index]] > 1 then
your_string = replace(your_string, your_string[index],"")
index = 0
else
index +=1
if index >= Len(your_string) then break
end if
end while