Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex character appearing exactly two times

Given an array of strings, test if there's any character appeared exactly two times.

Sample:

const input = ['asdf', 'fdas', 'asds', 'd fm', 'dfaa', 'aaaa', 'aabb', 'aaabb'];
const output = ['asds', 'dfaa', 'aabb', 'aaabb'];

I tried like this

/(?:^|(.)(?!\1))([A-Za-z0-9])\2(?!\2)/

but I got only these strings: ['dfaa', 'aabb', 'aaabb'].

Please any suggestions?

like image 789
GrayHat Avatar asked Mar 02 '23 10:03

GrayHat


2 Answers

You can use

/(.)(?<=^(?:(?!\1).)*\1(?=(?:(?!\1).)*\1(?:(?!\1).)*$))/s

See the regex demo. Note that since we are lookiing for the first match only, there is no g flag. Details:

  • (.) - Any one char captured into Group 1:
  • (?<= - start of a positive lookbehind, immediately on the left, there should be
    • ^ - start of string
    • (?:(?!\1).)* - any char, as many as possible, other than a char captured into Group 1
    • \1 - the same char captured into Group 1
    • (?=(?:(?!\1).)*\1(?:(?!\1).)*$) - a positive lookahead that requires any zero or more chars other than the char in Group 1, then the same char as captured in Group 1, and again any zero or more chars other than the char in Group 1 till end of string
  • ) - end of the lookbehind.

The lookarounds with tempered greedy tokens inside ensure the captured char only appears exactly twice in a string.

See the JavaScript demo:

const input = ['asdf', 'fdas', 'asds', 'd fm', 'dfaa', 'aaaa', 'aabb', 'aaabb'];
const re = /(.)(?<=^(?:(?!\1).)*\1(?=(?:(?!\1).)*\1(?:(?!\1).)*$))/s;
for (const s of input) {
   console.log(s, '=>', re.test(s))
}
like image 169
Wiktor Stribiżew Avatar answered Mar 04 '23 00:03

Wiktor Stribiżew


For Example, /[^a]/gm: Match a single character not present in the list below.

const sampleArray = [ 'asdf', 'fdas', 'asds', 'd fm', 'dfaa', 'aaaa', 'aabb', 'aaabb'];

var sampleOutput = [];

sampleArray.forEach(function (item, index) {
  var dupResult = checkDuplicate(item);
  if(dupResult){
    sampleOutput.push(item);
  }
});

console.log(sampleOutput);

function checkDuplicate(item){
    var isCountTwice = false;
    for(var i = 0; i < item.length; i++){
        if(item.replace(new RegExp("[^"+ item[i] +"]","gm"), "").length == 2){
            isCountTwice = true;
        }
    } 
    return isCountTwice;
}
like image 39
TBA Avatar answered Mar 03 '23 22:03

TBA