When we are doing string processing, if the string handler does not implement what we want, we use the regular to help us implement it.
The general use of the regular situation is: matching, find, split, find and replace, we will use these cases in PHP and Python language to achieve, and make a comparison. PHP is used: Pcre style.
#1 match Math (and get results)(Note that this is to get a match result that differs from not getting the result) #coding: utf-8 import Re strs = ' I love p you y do you t know H o? ' n haha fe ha ' Patt = re.compile (R ' ^.*? ( \w+). *?$ ', re. I) Print Patt.match (STRs). Group (1) #输出 P
Php:
#输出:string ' P ' (length=1)
Description: Preg_match (), like match in Python, will stop the search after the first match. and Preg_match_all () is different from this, it searches subject until it reaches the end.
in fact, the expression in PHP can also be:
Preg_match ('/(\w+)/', $strs, $m);
Find search #2 searches
Python:
Patt = Re.compile (R ' (\w+) ', Re. I) Print Patt.search (STRs). Group (1) #输出 P
As with the search method, if you find it and return it immediately, you will be searching for the end of the string and you can use Preg_match (_all) in PHP.
PHP:
Ibid .
#3 Matching Segmentation
Python:
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 ""
in PHP, you can use Preg_split () to implement
PHP:
/** output:
Array (length=6) (length=3) (length=3) (length=3) (length=3) (length=3) (length=6) (length=3)
**/
#4 Search Find all results (all)Python:
Print Patt.findall (STRs) #输出 [' P ', ' y ', ' t ', ' h ', ' o ', ' n ', ' FE ']
Preg_match_all () can be used in PHP to implementPHP:
/**
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 Preg_filter () in PHP, where Preg_filter () and Preg_replace () perform a search and replace of a regular expression. In the Python method, the lookup substitution is: Sub () and Subn (). Note that a new string returned by sub () does not work on the original object. subn () returns a tuple that consists of a "number 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) Replace with 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 ') # Output:
(length=51)
Another note 1 for the basic knowledge of the regular can Google, Python regular basic knowledge can also Google a bit. 2 For more on the regular basis of PHP pcre style, you can see: http://cn2.php.net/manual/zh/regexp.introduction.php3 Another point to note is that for processing strings that can be handled with string functions, use a function to handle them, and never use regular. http://www.bkjia.com/PHPjc/735170.html www.bkjia.com true http://www.bkjia.com/PHPjc/735170.html techarticle When we are doing string processing, if the string handler does not implement what we want, we use the regular to help us implement it. General use of regular conditions are: matching ...