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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:25:55+00:00 2026-05-29T13:25:55+00:00

I am having some trouble searching for a multiline pattern using regex. Here is

  • 0

I am having some trouble searching for a multiline pattern using regex. Here is the sample multiline string:

some command [first line]\n
second line \n
yes can have multiple lines\n
\n
something else that I do not care about.

Here is what I have tried so far:

>>> match = re.match(r"^(.+)\n((.*\n)*)\n",body,re.MULTILINE)
>>> match.groups()
('some command [first line]', 'second line \nyes can have multiple lines\n', 'yes can have multiple lines\n')

I am looking for match.group(1) and match.group(2), and I am happy with them, but it is bugging me that I get match.group(3) which I do not expect (and makes me this that my regexp is not right).

Also, I do not seem to get named patterns right..

 match = re.match(r"^(.+)\n((?P<bd>.*\n)*)\n",body,re.MULTILINE)
 >>> match.group(bd)
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 NameError: name 'bd' is not defined

I went through the Python Regular Expressions from Google, but it is obvious that I have not gotten the complete picture yet.

  • 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-29T13:25:55+00:00Added an answer on May 29, 2026 at 1:25 pm

    Did I understand you right, that the result that you expect is in group 3 instead of group2?

    If that is your problem, you can make groups non-capturing by putting a ?: at the start like this

    re.match(r"^(.+)\n(?:(.*\n)*)\n",body,re.MULTILINE)
    

    With this you will get only two groups in the result.

    Maybe I got you wrong and you want to get rid of the group 3, then

    re.match(r"^(.+)\n((?:.*\n)*)\n",body,re.MULTILINE)
    

    would be the solution.

    Named groups

    You can access your named group like this

    m.group('bd')
    

    you need to give group() either a integer or a string as argument, see MatchObject

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

Sidebar

Related Questions

Basically having some trouble with using Hover to hide or show an item. The
I'm having some trouble searching a SQLite database in my Android app. I use
I'm having some trouble searching for any similar match in two fields. For example
Im having some trouble reading xml files using jquery that containts swedish chars (åäö)
I'm having some trouble with databinding inside a UserControl when using an ItemsControl which
Having some trouble here with this. The setup: A select object with a choice
Having some trouble with what should be a very simple scenario. For example purposes,
Hey having some trouble trying to maintain transparency on a png when i create
Been having some trouble implementing a single-column index in LaTeX. I've been employing the
Im having some trouble with an oracle database. Every time i try to connect,

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.