Python: String search and Match, Re.compile () compile the regular expression string, and then use Match (), FindAll (), or finditer (), and so on

Source: Internet
Author: User

1. Using the Find () method

>>> Text = ' Yeah, but no, but yeah, but no, but yeah '

>>> text.find (' no ')
10

2. Using Re.match ()

for complex matches, you need to use regular expressions and re modules. To explain the basic principles of regular expressions, it is assumed that a date string that matches a number format, such as 11/27/2012, can be done:
>>> Text1 = ' 11/27/2012 '
>>> text2 = ' Nov, '
>>>
>>> Import Re
>>> # Simple matching: \d+ means match one or more digits
>>> if Re.match (R ' \d+/\d+/\d+ ', Text1):
... print (' Yes ')
.. else:
... print (' No ')
...
Yes
>>> if Re.match (R ' \d+/\d+/\d+ ', text2):
... print (' Yes ')
.. else:
... print (' No ')
...
No

3. Precompile the string Re.compile (), then match ()

If you want to use the same pattern to do multiple matches, you should first precompile the pattern string to a schema object. Like what:
>>> Datepat = re.compile (R ' \d+/\d+/\d+ ')
>>> if Datepat.match (Text1):
... print (' Yes ')
.. else:
... print (' No ')
...
Yes
>>> if Datepat.match (text2):

... print (' Yes ')
.. else:
... print (' No ')
...
No

4. Using the FindAll () method

Match () always starts from the string to match, if you want to find the pattern where any part of the string appears, use the FindAll () method instead. Like what:
>>> Text = ' Today is 11/27/2012. Pycon starts 3/13/2013. '
>>> Datepat.findall (text)
[' 11/27/2012 ', ' 3/13/2013 ']

5: Group () method to capture grouping


when you define a regular, you usually use parentheses to capture the grouping. For example:
>>> Datepat = Re.compile (R ' (\d+)/(\d+)/(\d+) ')
capturing a packet can make subsequent processing easier, because the contents of each group can be extracted separately.
For example:
>>> m = datepat.match (' 11/27/2012 ')
>>> M
<_sre. Sre_match object at 0x1005d2750>
>>> # Extract The contents of each group
>>> m.group (0)
' 11/27/2012 '
>>> M.group (1)
' One '
>>> M.group (2)
' A '
>>> M.group (3)
' + '
>>> m.groups ()
(' One ', ' + ', ' + ')
>>> Month, day, year = m.groups ()

>>> text
' Today is 11/27/2012. Pycon starts 3/13/2013. '
>>> Datepat.findall (text)
[(' 11 ', ' 27 ', ' 2012 '), (' 3 ', ' 13 ', ' 2013 ')]
>>> for month, day, year in Datepat.findall (text):
... print (' {}-{}-{} '. Format (year, month, day))
...
2012-11-27
2013-3-13

6. Iterate back Finditer ()

The FindAll () method searches for text and returns all matches as a list. If you want to return in an iterative way

>>> for M in Datepat.finditer (text):
... print (m.groups ())
...
(' 11 ', ' 27 ', ' 2012 ')
(' 3 ', ' 13 ', ' 2013 ')

The core step is to use Re.compile () to compile the regular expression string, and then use the match (), FindAll (), or Finditer () methods.

7. Exact match

If you want to match exactly, make sure your regular expression ends in $, like so:
>>> Datepat = Re.compile (R ' (\d+)/(\d+)/(\d+) $ ')
>>> datepat.match (' 11/27/2012abcdef ')
>>> datepat.match (' 11/27/2012 ')
<_sre. Sre_match Object at 0x1005d2750>
>>>

8. Simple text match/Search

Finally, if you are simply doing a simple text match/search operation, you can skip the compilation section and use the RE module-level functions directly. Like what:
>>> Re.findall (R ' (\d+)/(\d+)/(\d+) ', text)
[(' 11 ', ' 27 ', ' 2012 '), (' 3 ', ' 13 ', ' 2013 ')]
>>>
However, it is important to note that if you are going to do a lot of matching and search operations, it is a good idea to compile the regular expression first and then reuse it. A module-level function caches the most recently compiled schema, and therefore consumes too much performance, but if you use precompiled mode, you will reduce the lookup and some additional processing losses

Python: String search and Match, Re.compile () compile the regular expression string, and then use Match (), FindAll (), or finditer (), and so on

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.