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

  • Home
  • SEARCH
  • 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 929193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:07:45+00:00 2026-05-15T20:07:45+00:00

I’m in a situation where I need to parse arguments from a string in

  • 0

I’m in a situation where I need to parse arguments from a string in the same way that they would be parsed if provided on the command-line to a Java/Clojure application.

For example, I need to turn "foo \"bar baz\" 'fooy barish' foo" into ("foo" "bar baz" "fooy barish" "foo").

I’m curious if there is a way to use the parser that Java or Clojure uses to do this. I’m not opposed to using a regex, but I suck at regexes, and I’d fail hard if I tried to write one for this.

Any ideas?

  • 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-15T20:07:45+00:00Added an answer on May 15, 2026 at 8:07 pm

    Updated with a new, even more convoluted version. This is officially ridiculous; the next iteration will use a proper parser (or c.c.monads and a little bit of Parsec-like logic on top of that). See the revision history on this answer for the original.

    This convoluted bunch of functions seems to do the trick (not at my DRYest with this one, sorry!):

    (defn initial-state [input]
      {:expecting nil
       :blocks (mapcat #(str/split % #"(?<=\s)|(?=\s)")
                       (str/split input #"(?<=(?:'|\"|\\))|(?=(?:'|\"|\\))"))
       :arg-blocks []})
    
    (defn arg-parser-step [s]
      (if-let [bs (seq (:blocks s))]
        (if-let [d (:expecting s)]
          (loop [bs bs]
            (cond (= (first bs) d)
                  [nil (-> s
                           (assoc-in [:expecting] nil)
                           (update-in [:blocks] next))]
                  (= (first bs) "\\")
                  [nil (-> s
                           (update-in [:blocks] nnext)
                           (update-in [:arg-blocks]
                                      #(conj (pop %)
                                             (conj (peek %) (second bs)))))]
                  :else
                  [nil (-> s
                           (update-in [:blocks] next)
                           (update-in [:arg-blocks]
                                      #(conj (pop %) (conj (peek %) (first bs)))))]))
          (cond (#{"\"" "'"} (first bs))
                [nil (-> s
                         (assoc-in [:expecting] (first bs))
                         (update-in [:blocks] next)
                         (update-in [:arg-blocks] conj []))]
                (str/blank? (first bs))
                [nil (-> s (update-in [:blocks] next))]
                :else
                [nil (-> s
                         (update-in [:blocks] next)
                         (update-in [:arg-blocks] conj [(.trim (first bs))]))]))
        [(->> (:arg-blocks s)
              (map (partial apply str)))
         nil]))
    
    (defn split-args [input]
      (loop [s (initial-state input)]
        (let [[result new-s] (arg-parser-step s)]
          (if result result (recur new-s)))))
    

    Somewhat encouragingly, the following yields true:

    (= (split-args "asdf 'asdf \" asdf' \"asdf ' asdf\" asdf")
       '("asdf" "asdf \" asdf" "asdf ' asdf" "asdf"))
    

    So does this:

    (= (split-args "asdf asdf '  asdf \" asdf ' \" foo bar ' baz \" \" foo bar \\\" baz \"")
       '("asdf" "asdf" "  asdf \" asdf " " foo bar ' baz " " foo bar \" baz "))
    

    Hopefully this should trim regular arguments, but not ones surrounded with quotes, handle double and single quotes, including quoted double quotes inside unquoted double quotes (note that it currently treats quoted single quotes inside unquoted single quotes in the same way, which is apparently at variance with the *nix shell way… argh) etc. Note that it’s basically a computation in an ad-hoc state monad, just written in a particularly ugly way and in a dire need of DRYing up. 😛

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.