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

2

-1

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)
Zitella answered 10/2, 2023 at 9:2 Comment(1)
I made a snippet. Please make it a minimal reproducible exampleBook
C
0

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");
    }
}
Cacilie answered 10/2, 2023 at 9:12 Comment(2)
Thanks for the quick response but the solution above not giving the expected output in case I change the search string. If I give the search string as 'ab_ef' the output is ['ab/cd/ef','cd/ef,ab','ab/ef'] but the expected output is 'ab/ef' only i.e. solution should not contain any string other than what provided in search.Zitella
It is correct based on my understanding of your requirement. You should edit your question and tell us exactly what constitutes a match.Cacilie
B
0

We need to get some permutations going:

// Heap's algorithm https://mcmap.net/q/262859/-finding-all-permutations-of-array-elements-as-concatenated-strings
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']
Book answered 10/2, 2023 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.