Python Re.search method in Python

Source: Internet
Author: User
Re.searchScans the entire string and returns the first successful match, returns the Match object if the string contains the pattern substring, otherwise returns none, and note that if there are multiple pattern substrings in a string, only the first one is returned. Re.search () methodUsed to precisely match and extract the first conforming object, while extracting the object content uses Search MethodProperty Group () to implement the.

function Syntax:

Re.search (Pattern, string, flags=0)

Function parameter Description:

Parameters Describe
Pattern Matched regular Expressions
String The string to match
Flags A flag bit that controls how regular expressions are matched, such as case sensitivity, multiline matching, and so on.

The match succeeds Re.search method returns a matching object, otherwise none is returned.

We can use the group (NUM) or groups () matching object function to get a matching expression.

Match object method description
group (num=0) groups () Returns a tuple containing all the group strings, from 1 to the included group number.

Instance

#!/usr/bin/python#-*-coding:utf-8-*-Import reprint (re.search (' www ', ' www.runoob.com '). span ())  # matches print at the starting position ( Re.search (' com ', ' www.runoob.com '). span ())  # does not match at the start position

The above example runs the output as:

(0, 3) (11, 14)

In-Class extension:

What is the Python re.match function and read through the use of the Python match function

A Python regular expression that tells you what a python regular expression is?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.