chopper draw lion4
chopper draw lion4

Reputation: 13487

Regex: Finding non-duplicate characters

I have a string of letters:

'AAACDDBBK'

I want to match only characters which only repeat once, so for instance: C and K. I know brackets {} can be used to specify the number of matches you want, but that does not appear to be working for me.

str = 'AAACDDBBK'
regex = /[a-zA-Z]{1}/
str.match(regex)
>>>["A"]

How do I filter characters with more than one occurrence?

Upvotes: 0

Views: 3313

Answers (4)

Châu Hồng Lĩnh
Châu Hồng Lĩnh

Reputation: 1823

The solution of Federico Piazza above works. In Ruby, it would look like this:

st = 'aaabbceeefgh'

# All non-repeated characters:
st.gsub(/(.).*\1/,'') # => "cfgh"

# First non-repeated characters:
st.gsub(/(.).*\1/,'')[0] # => "c"

Upvotes: 0

Federico Piazza
Federico Piazza

Reputation: 30995

You can use a regex like this to find the duplicates:

(.).*\1

Then you can use a replace over your main string by an empty string, so your resulting string will have all the characters non duplicated

Working demo

The substitution section contains your resulting string having the non duplicated characters:

enter image description here

Btw, if you just want to find the non duplicated letters you can change the regex to:

([A-Za-z]).*\1

This solution, works for consecutive characters but if you can have duplicated characters then you should use another solution. What I'd do is to split your string by characters and add them into a map, then store for each character the count for their ocurrences. So, there you have another approach without regex.

Upvotes: 3

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

With Ruby or PHP you can use this kind of pattern:

(?:(.)\1+)*\K(.|$)

The idea is to find a unique character eventually preceded by one or several repeated character sequences, then to remove them using \K from the whole match result and to match the searched unique character with a simple . or the end of the string.

demo

An other possible way consists to use an alternation that begins with repeated characters:

(?:(.)\1+)+|(.)

not repeated characters are in the capture group 2.

Upvotes: 0

anubhava
anubhava

Reputation: 785156

This is a workaround for not having advanced PCRE features in Javascript:

str = 'AAACDDBBK'
str.replace(new RegExp(str.match(/([A-Z])(?=.*?\1)/ig).join('|'), "g"), "");
//=> CK

str="AAPAACDDBBK";
str.replace(new RegExp(str.match(/([A-Z])(?=.*?\1)/ig).join('|'), "g"), "");
//=> PCK

Upvotes: 0

Related Questions