nubeCoder
nubeCoder

Reputation: 9

Need Regular Expression to fetch string data from array with exact match

I want to match and store string in an array using javascript and need regular expression for this. Other solution also appreciated. separated by '_' if only both matches in string those string from array should be retured only, no other match should be accepted. Array and search string both are dynamic. Below is just an example but solution shoud match for any dynamic data. Example problem given below.

let arr1 = ['ef','cd','ab','cdab','efab','cdef','ab/cd/ef','cd/ef,ab','cd/ab','ab/ef']

test scenarios:

  1. let search = 'ef_ab'; expected output would be ['efab','ab/ef']
  2. let search = 'ab_cd_ef'; expected output would be ['ab/cd/ef','cd/ef,ab']
  3. let search = 'cd'; expected output would be ['cd']

Any help in javascript for problem is appreciated.

I have tried below regex and looping. Here word1 for given example could be ab or cd or ef and same could be for word2 , word3

let arr1 = ['ef', 'cd', 'ab', 'cdab', 'efab', 'cdef', 'ab/cd/ef', 'cd/ef,ab', 'cd/ab', 'ab/ef']

let regex = /(?=.*word1)(?=.*word2)(?=.*word3)/;

let arr2 = [];

for (i = 0; i < arr1.length; i++) {
  if (regex.test(arr1[i]))
    arr2.push(arr1[i]);
}
console.log(arr2)

Upvotes: -1

Views: 93

Answers (2)

mplungjan
mplungjan

Reputation: 178079

We need to get some permutations going:

// Heap's algorithm https://stackoverflow.com/a/66122464/295783
const excluding = (i) => (xs) => [... xs.slice (0, i), ... xs.slice (i + 1)]; 
const permutations = (xs) => xs.length == 0 ? [[]] : xs.flatMap ((x, i) => permutations (excluding (i) (xs)).map (p => (x +' '+ p).trim()));

const findSequence = (arr,str) => {
  const parts = str.split("_");
  const re = new RegExp(`^${permutations(parts)  // ^ from start
    .map(part => `${part.replace(/ /g,".?")}`)   // with a character or not in between
    .join('|')}$`);                              // to end $ of each string
  console.log(re); // just to show the resulting regexp
  return arr.filter(item => item.match(re)); 
}
let arr1 = ['ef', 'cd', 'ab', 'cdab', 'efab', 'cdef', 'ab/cd/ef', 'cd/ef,ab', 'cd/ab', 'ab/ef']

console.log(findSequence(arr1,'ef_ab')) // ['efab','ab/ef']
console.log(findSequence(arr1,'ab_cd_ef')) // ['ab/cd/ef','cd/ef,ab']
console.log(findSequence(arr1,'cd')) // ['cd']

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521457

You may use the input to form a series of regex patterns, each of which must match against the input string. An input which matches all regex patterns is a valid match.

var arr1 = ['ef','cd','ab','cdab','efab','cdef','ab/cd/ef','cd/ef,ab','cd/ab','ab/ef'];
var search = 'ab_cd_ef';
var parts = search.split("_");
for (var i=0; i < arr1.length; ++i) {
    var counter = 0;
    for (var p=0; p < parts.length; ++p) {
        var r = new RegExp("\\b" + parts[p] + "\\b");
        if (r.test(arr1[i])) {
            ++counter;
        }
    }
    if (counter == parts.length) {
        console.log(arr1[i] + " => MATCH");
    }
}

Upvotes: 0

Related Questions