0

Trying to build a regex to retrieve/select elements in an encoded string depending some keyboard signs order:

 str = "(abc-1é,d[efg_2ç;i|jkl+3ö.(mno?4à,p[qr(st!5',uv#6^;|xyz%7_." // look strange :)

I need to retrieve all parts included

1) in ( , -> "abc-1é" + "mno?4à" + "st!5'"

2) in [ ; -> "efg_2ç" + "qr(st!5,uv#6^"

3) in | . -> "jkl+3ö" + "xyz%7_"

I've tried a lot of possibilities on https://regex101.com/ and I'm able to produce some right results:

/\(([^()]*),/g -> catch "(abc-1é," "(mno?4à," and "(st!5'," -> OK

/\|[\w()]{0,}(...)./g -> catch "|jkl+3ö."  and  "|xyz%7_." -> OK

Don't ask me why but it works, except:

/\[([^()]*);/g  or /\[[\w()]{0,}(...);/g  -> catch "[efg_2ç;" but ignore "[qr(st!5',uv#6^;"

My question is: is it possible to create an unique regex able to capture the 3 signs-conditions? something like:

 / init: \[|\(|\| -> inside: [all founded signs, any length] -> end: ,|.|; /g

and (if possible) with some explanations... Thx in advance

3
  • OK, [qr(st!5',uv#6^; works well if signs ! ' and , are declared in the regex [[\w(),!']{0,}(...); Commented Jun 16, 2020 at 12:13
  • This mighty single regex, capable of matching everything at once, does not exist due to interferences of rivaling patterns. Use 3 patterns each limited to exactly its use case. Use it with matchAll results that get destructured/flattened into a single array and then reduced or mapped. This also helps readability and refactoring/maintenance. Commented Jun 16, 2020 at 13:11
  • OK. What I'm tying to do is to encode for storage a kind on contract built on if->then->else imbricated conditions to be reused later. For example (a:b,A[c>d;|B[c>d;. once decoded says if (number of votes > 10) { Approve(name,decision) } else { Reject(name,decision) } Each actor will be able to safely store its own conditions. Commented Jun 16, 2020 at 13:59

1 Answer 1

1

This mighty single regex, capable of matching everything at once, does not exist due to interferences of rivaling patterns. One might use 3 patterns instead, each limited to exactly its special use case.

A possible approach then makes use of matchAll for each pattern, does convert/destructure and collect each result-array into a single array and finally maps all the captures. Such an approach also is more readable and better for refactoring/maintenance ...

// (/(?:\((?<pc>[^,]*),)|(?:\[(?<bs>[^;]*);)|(?:\|(?<pp>[^\.]*)\.)/gm);

const regxParenthesesComma = (/\(([^,]*),/gm);
const regxBracketSemicolon = (/\[([^;]*);/gm);
const regxPipePeriod = (/\|([^\.]*)\./gm);

function extractCharacterSequences(str) {
  return [

    ...str.matchAll(regxParenthesesComma),
    ...str.matchAll(regxBracketSemicolon),
    ...str.matchAll(regxPipePeriod)

  ].map(result => result[1]);
}

const test = "(abc-1é,d[efg_2ç;i|jkl+3ö.(mno?4à,p[qr(st!5',uv#6^;|xyz%7_.";

console.log(
  'extractCharacterSequences(test) => ',
  extractCharacterSequences(test)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Sign up to request clarification or add additional context in comments.

6 Comments

wooooow @Peter :-) greaaaaaaaaaat
@Wolden ... what does "wooow" mean? Did the approach solve the problem? Is there anything that still needs to be improved?
perfectly solved, thanks :-) [except str.matchAll is not accepted by all browsers but it works as expected when working :-) ]
@Wolden ... is it the accepted approach then, or does the OP need a solution that works in a yet to be specified environment?
I've checked all options (accepted)(solved)(approved) :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.