Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9268809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:57:12+00:00 2026-06-18T14:57:12+00:00

I need to define a bunch of vector sequences, which are all a series

  • 0

I need to define a bunch of vector sequences, which are all a series of L,D,R,U for left, down, right, up or x for break. There are optional parts, and either/or parts. I have been using my own invented system for noting it down, but I want to document this for other, potentially non-programmers to read.

I now want to use a subset (I don’t plan on using any wildcards, or infinite repetition for example) of regex to define the vector sequence and a script to produce all possible matching strings…

/LDR/ produces ['LDR']
/LDU?R/ produces ['LDR','LDUR']
/R(LD|DR)U/ produces ['RLDU','RDRU']
/DxR[DL]U?RDRU?/ produces ['DxRDRDR','DxRDRDRU','DxRDURDR','DxRDURDRU','DxRLRDR','DxRLRDRU','DxRLURDR','DxRLURDRU']

Is there an existing library I can use to generate all matches?

EDIT

I realised I will only be needing or statements, as optional things can be specified by thing or nothing maybe a, or b, both optional could be (a|b|). Is there another language I could use to define what I am trying to do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:57:13+00:00Added an answer on June 18, 2026 at 2:57 pm

    By translating the java code form the link provided by @Dukeling into javascript, I think I have solved my problem…

    var Node = function(str){
        this.bracket = false;
        this.children = [];
        this.s = str;
        this.next = null;
        this.addChild = function(child){
            this.children.push(child);
        }
    }
    
    var printTree = function(root,prefix){
      prefix = prefix.replace(/\./g, "");
      for(i in root.children){
        var child = root.children[i]
        printTree(child, prefix + root.s);
      }
      if(root.children.length < 1){
        console.log(prefix + root.s);
      }
    }
    
    var Stack = function(){
        this.arr = []
        this.push = function(item){
            this.arr.push(item)
        }
        this.pop = function(){
            return this.arr.pop()
        }
        this.peek = function(){
            return this.arr[this.arr.length-1]
        }
    }
    
    var createTree = function(s){
    
        // this line was causing errors for `a(((b|c)d)e)f` because the `(((` was only
        // replacing the forst two brackets.
        // var s = s.replace(/(\(|\||\))(\(|\||\))/g, "$1.$2");
        // this line fixes it
        var s = s.replace(/[(|)]+/g, function(x){ return x.split('').join('.') });
    
        var str = s.split('');
        var stack = new Stack();
        var root = new Node("");
        stack.push(root); // start node
        var justFinishedBrackets = false;
        for(i in str){
            var c = str[i]
            if(c == '('){
                stack.peek().next = new Node("Y"); // node after brackets
                stack.peek().bracket = true; // node before brackets
            } else if (c == '|' || c == ')'){
                var last = stack.peek(); // for (ab|cd)e, remember b / d so we can add child e to it
                while (!stack.peek().bracket){ // while not node before brackets
                    stack.pop();
                }
                last.addChild(stack.peek().next); // for (b|c)d, add d as child to b / c
            } else {
                if (justFinishedBrackets){
                    var next = stack.pop().next;
                    next.s = "" + c;
                    stack.push(next);
                } else {
                    var n = new Node(""+c);
                    stack.peek().addChild(n);
                    stack.push(n);
                }
            }
            justFinishedBrackets = (c == ')');
        }
        return root;
    }
    
    // Test it out
    var str = "a(c|mo(r|l))e";
    var root = createTree(str);
    printTree(root, "");
    // Prints: ace / amore / amole
    

    I only changed one line, to allow more than two consecutive brackets to be handled, and left the original translation in the comments

    I also added a function to return an array of results, instead of printing them…

    var getTree = function(root,prefix){
      this.out = this.out || []
      prefix = prefix.replace(/\./g, "");
      for(i in root.children){
        var child = root.children[i]
        getTree(child, prefix + root.s, out);
      }
      if(root.children.length < 1){
        this.out.push(prefix + root.s);
      }
      if(!prefix && !root.s){
        var out = this.out;
        this.out = null
        return out;
      }
    }
    
    // Test it
    var str = "a(b|c)d";
    var root = createTree(str);
    console.log(getTree(root, ""));
    // logs ["abd","acd"]
    

    The last part, to allow for empty strings too, so… (ab|c|) means ab or c or nothing, and a convenience shortcut so that ab?c is translated into a(b|)c.

    var getMatches = function(str){
        str = str.replace(/(.)\?/g,"($1|)")
        // replace all instances of `(???|)` with `(???|µ)`
        // the µ will be stripped out later
        str = str.replace(/\|\)/g,"|µ)")
        // fix issues where last character is `)` by inserting token `µ`
        // which will be stripped out later
        str = str+"µ"
        var root = createTree(str);
        var res = getTree(root, "");
        // strip out token µ
        for(i in res){
            res[i] = res[i].replace(/µ/g,"")
        }
        // return the array of results
        return res
    }
    
    getMatches("a(bc|de?)?f");
    // Returns: ["abcf","adef","adf","af"]
    

    The last part is a little hack-ish as it relies on µ not being in the string (not an issue for me) and solves one bug, where a ) at the end on the input string was causing incorrect output, by inserting a µ at the end of each string, and then stripping it from the results. I would be happy for someone to suggest a better way to handle these issues, so it can work as a more general solution.

    This code as it stands does everything I need. Thanks for all your help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to define an <img>'s src attribute in CSS. Is there a way
Why is there a need to define a new method in RESTful controller, follow
Although appengine already is schema-less, there still need to define the entities that needed
I need to define a Structuring Element of a mask size 30 by 105
I need to define the constant in the module that use the method from
I need to define a Wix file component that may not exist in certain
I need to define an appender for log4net in a way that I get
I need to define some constant strings that will be used only by one
I need to define multiple modules in the same file. I would like to
I'm using CakePHP 2.1 and need to define an Inflector rule for the word

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.