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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:09:56+00:00 2026-05-12T20:09:56+00:00

I am trying to parse a list of items which satisfies the python regex

  • 0

I am trying to parse a list of items which satisfies the python regex

r'\A(("[\w\s]+"|\w+)\s+)*\Z'

that is, it’s a space separated list except that spaces are allowed inside quoted strings. I would like to get a list of items in the list (that is of items matched by the

r'("[\w\s]+"|\w+)'

part. So, for example

>>> parse('foo "bar baz" "bob" ')
['foo', '"bar baz"', '"bob"']

Is there any nice way to do this with python re?

Many things don’t quite work. For example

>>> re.match(r'\A(("[\w\s]+"|\w+)\s+)*\Z', 'foo "bar baz" "bob" ').group(2)
'"bob"'

only returns the last one it matched. On the other hand

>>> re.findall(r'("[\w\s]+"|\w+)', 'foo "bar baz" "bob" ')
['foo', '"bar baz"', '"bob"']

but it also accepts malformed expressions like

>>> re.findall(r'("[\w\s]+"|\w+)', 'foo "bar b-&&az" "bob" ')
['foo', 'bar', 'b', 'az', '" "', 'bob']

So is there any way to use the original regex and get all of the items that matched group 2? Something like

>>> re.match_multigroup(r'\A(("[\w\s]+"|\w+)\s+)*\Z', 'foo "bar baz" "bob" ').group(2)
['foo', '"bar baz"', '"bob"']
>>> re.match_multigroup(r'("[\w\s]+"|\w+)', 'foo "bar b-&&az" "bob" ')
None

Edit: It is important that I preserve the quotes in the output, thus I don’t want

>>> re.match_multigroup(r'\A(("[\w\s]+"|\w+)\s+)*\Z', 'foo "bar baz" "bob" ').group(2)
['foo', 'bar baz', 'bob']

because then I don’t know if bob was quoted or not.

  • 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-12T20:09:57+00:00Added an answer on May 12, 2026 at 8:09 pm

    Alright, I ended up deciding to do this in two steps.

    First I check that the expression is syntactically valid and second I break it into individual pieces:

    def parse(expr):
        if re.match(r'\A(("[\w\s]+"|\w+)\s+)*\Z', expr):
            return re.findall(r'("[\w\s]+"|\w+)', expr)
    

    So:

    >>> parse('foo "bar baz" "bob" ')
    ['foo', '"bar baz"', '"bob"']
    >>> parse('foo "bar b-&&az" "bob" ')
    >>> parse('foo "bar" ')
    ['foo', '"bar"']
    >>> parse('"foo" bar ')
    ['"foo"', 'bar']
    >>> parse('foo"bar baz" "bob" ')
    >>> parse('&&')
    

    I’m about 90% sure that this method works appropriately for all strings, but I would still be interested if anyone had a more general solution, this seems sort of kludgey to me.

    Thanks SilentGhost and Alan Moore for the help. I did not know about python csv or regex lookaheads before, it might be helpful to me to learn about those.

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

Sidebar

Ask A Question

Stats

  • Questions 212k
  • Answers 212k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sounds more like you'd want to create a virtual printer… May 12, 2026 at 10:18 pm
  • Editorial Team
    Editorial Team added an answer Well, the simplest way is to take the average of… May 12, 2026 at 10:18 pm
  • Editorial Team
    Editorial Team added an answer Looking at your code, it seems all fine and your… May 12, 2026 at 10:18 pm

Related Questions

I am trying to parse Twitter RSS feeds and put the information in a
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? Is there
We are beginning the process of moving from Web Forms to MVC for all
I am parsing a HTML document with XPATH and I want to keep all

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.