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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:01:15+00:00 2026-05-10T17:01:15+00:00

What is the difference between the search() and match() functions in the Python re

  • 0

What is the difference between the search() and match() functions in the Python re module?

I’ve read the Python 2 documentation (Python 3 documentation), but I never seem to remember it.

  • 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. 2026-05-10T17:01:15+00:00Added an answer on May 10, 2026 at 5:01 pm

    re.match is anchored at the beginning of the string. That has nothing to do with newlines, so it is not the same as using ^ in the pattern.

    As the re.match documentation says:

    If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

    Note: If you want to locate a match anywhere in string, use search() instead.

    re.search searches the entire string, as the documentation says:

    Scan through string looking for a location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

    So if you need to match at the beginning of the string, or to match the entire string use match. It is faster. Otherwise use search.

    The documentation has a specific section for match vs. search that also covers multiline strings:

    Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).

    Note that match may differ from search even when using a regular expression beginning with '^': '^' matches only at the start of the string, or in MULTILINE mode also immediately following a newline. The “match” operation succeeds only if the pattern matches at the start of the string regardless of mode, or at the starting position given by the optional pos argument regardless of whether a newline precedes it.

    Now, enough talk. Time to see some example code:

    # example code: string_with_newlines = '''something someotherthing'''  import re  print re.match('some', string_with_newlines) # matches print re.match('someother',                 string_with_newlines) # won't match print re.match('^someother', string_with_newlines,                 re.MULTILINE) # also won't match print re.search('someother',                  string_with_newlines) # finds something print re.search('^someother', string_with_newlines,                  re.MULTILINE) # also finds something  m = re.compile('thing$', re.MULTILINE)  print m.match(string_with_newlines) # no match print m.match(string_with_newlines, pos=4) # matches print m.search(string_with_newlines,                 re.MULTILINE) # also matches 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If that element has vertical padding or margin, it’s added… May 11, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer var numbers = sNumbers?.Split(',')?.Select(Int32.Parse)?.ToList(); Recent versions of C# (v6+) allow… May 11, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer Emailing it is easy. Pick a library in your favorite… May 11, 2026 at 6:52 pm

Related Questions

I've noticed a lot of talk about asp.net MVC lately, but I haven't come
What is the difference between storing a datatable in Session vs Cache? What are
What does it take for online documentation to be helpful and interesting to read?
A while ago I had a query that I ran quite a lot for

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.