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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:08:40+00:00 2026-05-25T01:08:40+00:00

I can’t find how to proceed for a regular expression, here is an example:

  • 0

I can’t find how to proceed for a regular expression, here is an example:

string = "red\\/banana 36    monkey\\/apple 14   red\\/apple 23  red\\/horse 56  bull\\/red 67  monkey\\/red 45    bull\\/shark 89"

I want to do a single regex with re.match.group() which will take into account only the ones like red/xxxx and the ones like xxxx/red and group the xxxx names only, not couples:

I want to do:

print(match.group("beginningwithred") + " " + match.group("number")

and obtain:

banana 36
apple 23
horse 56

then do:

print(match.group("endingwithred") + " " + match.group("number")

and obtain:

bull 67
monkey 45

my current code goes like:

iterator = regex.finditer(string)
for match in iterator:
    regex = re.compile('red\\\\\\\\/(?P<beginningwithred>banana|apple|horse)|(?P<endingwithred>bull|monkey)\\\\\\\\/red (?P<number>\d\d)')

but it doesn’t work, I can’t use | between groups and python HOWTO doesn’t help..
I tried with { } too including the whole two expressions but it doesn’t work either.
It must not be really complicated but I can’t find out what’s wrong.

  • 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-25T01:08:41+00:00Added an answer on May 25, 2026 at 1:08 am

    i don’t completely follow, but it sounds like you want non-capturing groups around your alternatives:

    (?:foo|bar|baz)
    

    that lets you use | without creating a “real” group.


    update why doesn’t this help? is this not right?

    >>> s="red\\/banana 36    monkey\\/apple 14   red\\/apple 23  red\\/horse 56  bull\\/red 67  monkey\\/red 45    bull\\/shark 89"
    >>> r = re.compile(r'(?:red\\/(?P<begin>\w+)|(?P<end>\w+)\\/red)\s+(?P<number>\d+)')
    >>> for m in r.finditer(s):
    ...     print(m.groups())
    
    ('banana', None, '36')
    ('apple', None, '23')
    ('horse', None, '56')
    (None, 'bull', '67')
    (None, 'monkey', '45')
    

    update2

    if you just want to print out the non-None values you can do something like:

      >>> for m in r.finditer(s):
      ...     print(','.join(g for g in m.groups() if g is not None))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody tell me a regular expression to use within some PHP to find
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can we put functions to determine the condition for our list comprehensions. Here is
Can anyone help me trying to find out why this doesn't work. The brushes
Does anyone know how can I replace this 2 symbol below from the string
Can someone explain dependency injection with a basic .NET example and provide a few
Can someone explain the usage of the dollar sign here.. var updateProgressDiv = $get('updateProgressDiv');
Can the Application Name passed to SQL Server (via the ADO Connection string) be
Can I perform 'merge' that updates only existing files in current branch? For example,

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.