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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:19:36+00:00 2026-06-05T03:19:36+00:00

I’m trying to parse a string with unknown number of elements with RegEx in

  • 0

I’m trying to parse a string with unknown number of elements with RegEx in Python. Here is the example:

>>>> import re
>>>> re.match("\=( A([0-9]+))*", "= A1 A2 A3 A4").groups()[1::2]
('4',)

I expect to have:

('1', '2', '3', '4',)

How can I get the expected result?

EDIT:

re.findall will not work for me. Let me make a better example:

I want to match the following string:

_func(cmd, param1, param2, param3, param4)_

I don’t know in advance the nummber of parameters. I expected to solve it using the following code:

>>> re.match("(\w+)\(cmd(, (\w+))*\)", "func(cmd, param1, param2, param3, param4)")

But this do not work, since groups ()* are not expanded to many items, but only last is used. 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-06-05T03:19:37+00:00Added an answer on June 5, 2026 at 3:19 am
    pat = re.compile(r' A(\d+)')
    lst = re.findall(pat, "= A1 A2 A3 A4")
    

    This returns a list, and in your example you showed a tuple. I presume a list will work for you, but of course you can always do:

    t = tuple(lst)
    

    The answer I just gave doesn’t actually check for the = in the input string. If you need to do that, you can always use two patterns and two steps:

    pat0 = re.compile(r'=(?: A\d+)+')
    pat1 = re.compile(r' A(\d+)')
    
    m = pat0.search("= A1 A2 A3 A4")
    if not m:
        print("input string not what was expected")
    else:
        s = m.group(0)
        lst = re.findall(pat, s)
    

    EDIT: Code that handles your func() example:

    s_code = "func(cmd, param1, param2, param3, param4)"
    pat_recognize_args = re.compile(r'func\(cmd([^)]*)\)')
    pat_parse_args = re.compile(r'[, ]+([^, ]+)')
    
    m = pat_recognize_args.search(s_code)
    if m:
        s = m.group(1)
        lst = re.findall(pat_parse_args, s)
    

    When I ran the above code, lst was set to: ['param1', 'param2', 'param3', 'param4']

    pat_recognize_args looks for the literal string func with a literal ( (which is backslash-escaped in the pattern so re won’t try to use it to start a match group), then the literal string cmd, and then a match group that matches anything up to a literal ) character; then the match group is closed with a ) and a literal ) is there to match the actual ) that finishes the function call. After this pattern matches, the match object will have group 1 set to just the interesting arguments from the function call.

    So next we set s = m.group(1) and then have re.findall() pull out the arguments for us.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
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

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.