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 642385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:09:55+00:00 2026-05-13T21:09:55+00:00

I have a string input = "maybe (this is | that was) some ((nice

  • 0

I have a string

input = "maybe (this is | that was) some ((nice | ugly) (day |night) | (strange (weather | time)))"

How is the best method in Ruby to parse this string ?

I mean the script should be able to build sententes like this :

maybe this is some ugly night

maybe that was some nice night

maybe this was some strange time

And so on, you got the point…

Should I read the string char by char and bulid a state machine with a stack to store the parenthesis values for later calculation, or is there a better approach ?

Maybe a ready, out of the box library for such purpose ?

  • 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-05-13T21:09:56+00:00Added an answer on May 13, 2026 at 9:09 pm

    Try Treetop. It is a Ruby-like DSL to describe grammars. Parsing the string you’ve given should be quite easy, and by using a real parser you’ll easily be able to extend your grammar later.

    An example grammar for the type of string that you want to parse (save as sentences.treetop):

    grammar Sentences
      rule sentence
        # A sentence is a combination of one or more expressions.
        expression* <Sentence>
      end
    
      rule expression
        # An expression is either a literal or a parenthesised expression.
        parenthesised / literal
      end
    
      rule parenthesised
        # A parenthesised expression contains one or more sentences.
        "(" (multiple / sentence) ")" <Parenthesised>
      end
    
      rule multiple
        # Multiple sentences are delimited by a pipe.
        sentence "|" (multiple / sentence) <Multiple>
      end
    
      rule literal
        # A literal string contains of word characters (a-z) and/or spaces.
        # Expand the character class to allow other characters too.
        [a-zA-Z ]+ <Literal>
      end
    end
    

    The grammar above needs an accompanying file that defines the classes that allow us to access the node values (save as sentence_nodes.rb).

    class Sentence < Treetop::Runtime::SyntaxNode
      def combine(a, b)
        return b if a.empty?
        a.inject([]) do |values, val_a|
          values + b.collect { |val_b| val_a + val_b }
        end
      end
    
      def values
        elements.inject([]) do |values, element|
          combine(values, element.values)
        end
      end
    end
    
    class Parenthesised < Treetop::Runtime::SyntaxNode
      def values
        elements[1].values
      end
    end
    
    class Multiple < Treetop::Runtime::SyntaxNode
      def values
        elements[0].values + elements[2].values
      end
    end
    
    class Literal < Treetop::Runtime::SyntaxNode
      def values
        [text_value]
      end
    end
    

    The following example program shows that it is quite simple to parse the example sentence that you have given.

    require "rubygems"
    require "treetop"
    require "sentence_nodes"
    
    str = 'maybe (this is|that was) some' +
      ' ((nice|ugly) (day|night)|(strange (weather|time)))'
    
    Treetop.load "sentences"
    if sentence = SentencesParser.new.parse(str)
      puts sentence.values
    else
      puts "Parse error"
    end
    

    The output of this program is:

    maybe this is some nice day
    maybe this is some nice night
    maybe this is some ugly day
    maybe this is some ugly night
    maybe this is some strange weather
    maybe this is some strange time
    maybe that was some nice day
    maybe that was some nice night
    maybe that was some ugly day
    maybe that was some ugly night
    maybe that was some strange weather
    maybe that was some strange time
    

    You can also access the syntax tree:

    p sentence
    

    The output is here.

    There you have it: a scalable parsing solution that should come quite close to what you want to do in about 50 lines of code. Does that help?

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

Sidebar

Related Questions

I have some input that looks like the following: A,B,C,"D12121",E,F,G,H,"I9,I8",J,K The comma-separated values can
I have a string input that i do not know whether or not is
I have string like this : <input name=my_name id=my_id value=my_value class=my_class /> I would
I have the following JSON string coming from external input source: {value: "82363549923gnyh49c9djl239pjm01223", id:
For example, I have input parameter this format: " 04:00-06:00 " or " 23:00-24:00
I have as input a string that is a URI . how is it
I have a string input from which I need to extract simple information, here
I have a string as input and have to break the string in two
I have an input string as follows: thumb_634735515600845357tchayat_november_200612.jpg What I want to do is
I have an input string which is a directory address: Example: ProgramFiles/Micro/Telephone And I

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.