PHP, Python related regular functions one point example

Source: Internet
Author: User
Tags array object expression functions implement php and regular expression split

When we are doing string processing, if string processing functions do not implement what we want, we use the positive to help us achieve it.

General use of regular conditions are: matching, find, split, find and replace, the following we will use the PHP and Python language to achieve, and do a comparison. PHP is used: Pcre style.
#1 match Math (and get results) (Note that this is to get a matching result that is different from not getting the result)

Python:
		

#coding: utf-8 import Re strs = ' I love p you y you t know H o? n hahaha fe ha ' Patt = re.compile (R ' ^.*? \w+). *?$ ', re. I) Print Patt.match (STRs). Group (1) #输出 P

Shows that match is a matching process, not a lookup. This method does not match exactly, and if you want to match exactly, you can add the boundary match ' $ ' at the end of the expression. PHP:
		

<?php $strs = ' I love p you y you t know H o? n haha fe ha '; Preg_match ('/^.*? \w+). *?$/i ', $strs, $m); Var_dump ($m [1]);

#输出:string ' P ' (length=1)

Description: Preg_match (), like match in Python, will stop the search after the first match. Unlike this, Preg_match_all () searches subject until it reaches the end. In fact, you can also do this in a php-positive expression:
		

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

#2 Search to find searchPython:
		

Patt = Re.compile (R ' (\w+) ', Re. I) Print Patt.search (STRs). Group (1) #输出 P

Description of the search method, if you find it immediately return, otherwise search to the end of the string, in PHP can use Preg_match (_all) to achieve. PHP: Ditto #3 Matching SegmentationPython:
		

Patt = Re.compile (R ' \w+ ', re. i) for I in Patt.split (STRs): #注意这里要使用unicode对象输出 print Unicode (i, ' utf-8 ') #以上输出 ' I love you, you know? Hahaha ' '

You can use Preg_split () in PHP to implement PHP:
		

<?php $strs = ' I love p you y you t know H o? n haha fe ha '; $m = Preg_split ('/\w+/i ', $strs); Var_dump ($m);

/** output:

		Array
  (length=6)
  (length=3)
  (length=3)
  (length=3)
  (length=3)
  (length=3)
  (length=6)
  (length=3)

**/

Find All Results #4 search (All)Python:
		

Print Patt.findall (STRs) #输出 [' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ' FE ']

You can use Preg_match_all () in PHP to implement PHP:
			

<?php $strs = ' I love p you y you t know H o? n haha fe ha '; Preg_match_all ('/(\w+)/I ', $strs, $m); Var_dump ($m);

/**

			Array
  0 => 
    array(
      length=1)
      (length=1)
      (length=1)
       (length=1)
      (length=1)
      (length=1)
      (length=2)
  1 => 
    array(
      length=1)
      (length=1)
      (length=1)
      (length=1)
      (length=1)
      (length=1)
      (length=2)

**/

#5 Find ReplacementsIn fact, the Finditer () method is not a lookup substitution in Python, it is simply an iterator that returns a sequential access to each matching result (Match object) python:
		

For I in Patt.finditer (STRs): Print i.group () #以上输出 ' ' P y t h o n fe '

This differs from the Preg_filter () in PHP, Preg_filter () and preg_replace () are searches and replacements that perform a regular expression. In the Python method, a sub () and SUBN () are used to find replacements. Note that a new string returned by the sub () is not acting on the original object. SUBN () returns a tuple of "new strings and replacements", and does not work on the original object.
		

#替换三次 print patt.sub (' strs,3 ') #输出 ' I love 99 you 99 you 99 know H o? n haha fe ha '

Print patt.subn (' STRs ') #输出: is a tuple (' I love 99 you 99 you 99 know 99 99? 99 haha 99 ha ', 7)

Substitution and Reference
			

#这里批量替换文章中的图片的路径 (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:
		

		

#这里批量替换文章中的图片的路径 (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")

#输出:

		(length=51)
Another note 1 for regular basics can be Google, and Python's basic knowledge can also be Google. 2 for more on the regular basis of PHP pcre style, see: http://cn2.php.net/manual/zh/regexp.introduction.php 3 Another point to note is that processing strings can be handled with string functions To use the function of processing, do not use regular.



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.