Example of PHP and Python related regular functions

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. When we are doing string processing, if the string processing function cannot implement what we want, we use regular expressions to help us achieve this. Regular Expressions are usually used in the following scenarios: matching, searching, splitting, searching, and replacing. Below we will use PHP and Pyt respectively.

Welcome to the Linux community forum to interact with 2 million technical staff> when we are doing string processing, if the string processing function cannot implement what we want, we use regular expressions to help us achieve this. Regular Expressions are usually used in the following scenarios: matching, searching, splitting, searching, and replacing. Below we will use PHP and Pyt respectively.

Welcome to the Linux community forum and interact with 2 million technicians>

When we are doing string processing, if the string processing function cannot implement what we want, we will use regular expressions to help us implement it.

Regular Expressions are generally used in the following scenarios: matching, searching, splitting, searching, and replacement. Below we will use PHP and Python respectively for implementation and comparison.

PHP Regular Expressions adopt the PCRE style.

#1 match the Math (and obtain the result) (note that the matching result must be obtained here, which is different from the result not obtained) # coding: UTF-8 import re strs = 'I love P you y do you t know h o? N Haha fe har' patt = re. compile (R' ^ .*? (\ W + ).*? $ ', Re. I) print patt. match (strs). group (1) # output P

PHP:

  

# Output: string 'P' (length = 1)

Note: preg_match () is the same as match in python. After the first match, the search will stop. Preg_match_all () is different from this. It searches for subject until it reaches the end.

In fact, the regular expression in PHP can also be like this:

Preg_match ('/(\ w +)/', $ strs, $ m );

#2 Search for Search

Python:

Patt = re. compile (R' (\ w +) ', re. I) print patt. search (strs). group (1) # output P

It indicates that the search method is the same. If the search method is found, it will be returned immediately. Otherwise, it will be searched until the end of the string. You can use preg_match (_ all) in PHP to achieve this.

PHP:

Same as above

#3 matching and Segmentation

Python:

Patt = re. compile (R' \ w + ', re. i) for I in patt. split (strs): # Use the unicode object to output print unicode (I, 'utf-8') # output ''' above. Do I love you? Haha '''

You can use preg_split () in PHP to implement

PHP:

  

/** Output:

Array

0 => string 'love' (length = 6)

1 => string 'you' (length = 3)

2 => string 'you' (length = 3)

3 => string 'zhi' (length = 3)

4 => string '?' (length = 3)

5 => string '? '(Length = 3)

6 => string 'haha '(length = 6)

7 => string 'har' (length = 3 )**/

#4 search for ALL results (ALL)

Python:

Print patt. findall (strs) # output ['P', 'y', 't', 'h', 'O', 'n', 'fe ']

You can use preg_match_all () in PHP to implement

PHP:

  

/**

Array

0 =>

Array

0 => string 'P' (length = 1)

1 => string 'y' (length = 1)

2 => string 'T' (length = 1)

3 => string 'H' (length = 1)

4 => string 'O' (length = 1)

5 => string 'n' (length = 1)

6 => string 'fe '(length = 2)

1 =>

Array

0 => string 'P' (length = 1)

1 => string 'y' (length = 1)

2 => string 'T' (length = 1)

3 => string 'H' (length = 1)

4 => string 'O' (length = 1)

5 => string 'n' (length = 1)

6 => string 'fe '(length = 2)

**/

#5 search and replace

In fact, the finditer () method is not a search replacement in python. It is only an iterator that returns a sequential access to each matching result (Match object ).

Python:

For I in patt. finditer (strs): print I. group () # output ''p y t h o n fe '''

This is different from preg_filter () in PHP. Both preg_filter () and preg_replace () execute a regular expression to search and replace. In python, the regular method is used to replace sub () and subn ().

Note that a new string returned by sub () does not apply to the original object.

Subn () returns a new string and the number of replicas. It does not apply to the original object. # Replace print patt. sub ('99', strs, 3) three times # output 'I love 99 you 99 DO YOU KNOW h o? N Haha fe har' print patt. subn ('99', strs) # output: it is a tuple ('I love 99, do you know 99 99, 99? 99 Haha 99 Haha, 7) replace and reference

# Here, we will replace the path of images in the article in batches (old_c is the content of the article)

Img_dir = 'test'

Img_patt = re. compile ('src = ".*? /(\ W + \. \ w + )"')

New_c = img_patt.sub (r 'src = "./% s/\ 1" '% img_dir, old_c) PHP:

# Here, we will replace the path of images in the article in batches (old_c is the content of the article)

Img_dir = 'test' img_patt = re. compile ('src = ".*? /(\ W + \. \ w +) "') new_c = img_patt.sub (r 'src ="./% s/\ 1 "' % img_dir, old_c) # output:

String 'I love 999, you 999, do you know 999, 999? 999 Haha 999 har' (length = 51)

Note 1 For the basic knowledge of regular expressions, You can GOOGLE the basic knowledge of Python regular expressions.

2

Related Article

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.