Home Backend Development PHP Tutorial Some examples of regular functions related to PHP and Python_PHP tutorial

Some examples of regular functions related to PHP and Python_PHP tutorial

Jul 13, 2016 am 10:37 AM
php python cannot function deal with string Example accomplish us hour regular Related want

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

The general situations in which regular expressions are used include: matching, search, segmentation, search and replace. Below we will implement these situations in PHP and Python respectively, and make a comparison. PHP regular adopts: PCRE style.
#1 Match Math (and get the result)(Note that here you need to get the matching result, which is different from not getting the result) #coding:utf-8 import re strs = 'I love you, do you know? nhahafeha' 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. It will stop searching after the first match. Preg_match_all() is different from this, it will search the subject until it reaches the end. Actually, regular expressions in PHP can also look like this:

preg_match('/(w+)/',$strs,$m);

#2 SearchSearchPython:

patt = re.compile(r'(w+)',re.I) print patt.search(strs).group(1) #Output P

The search method is the same. If it is found, it will be returned immediately. Otherwise, it will be searched until the end of the string. In PHP, you can use preg_match(_all) to achieve this. PHP: Same as above#3 Match segmentationPython:

patt = re.compile(r'w+',re.I) for i in patt.split(strs): #Note that unicode object output is used here print unicode(i,'utf-8') #The above output '''I love you, do you know? Hahaha'''

You can use preg_split() in PHP to achieve this PHP:

/**Output:


<i><b>array</b>
  0 => string &#39;我爱&#39; <i>(length=6)</i>
  1 => string &#39;你&#39; <i>(length=3)</i>
  2 => string &#39;你&#39; <i>(length=3)</i>
  3 => string &#39;知&#39; <i>(length=3)</i>
  4 => string &#39;吗&#39; <i>(length=3)</i>
  5 => string &#39;?&#39; <i>(length=3)</i>
  6 => string &#39;哈哈&#39; <i>(length=6)</i>
  7 => string &#39;哈&#39; <i>(length=3)</i></i>
Copy after login
**/

#4 Search to find all results (ALL) Python:

print patt.findall(strs) #Output ['P', 'y', 't', 'h', 'o', 'n', 'fe']

Preg_match_all() can be used in PHP to achieve PHP:


/**

<i><i><b>array</b>
  0 => 
    <b>array</b>
      0 => string &#39;P&#39; <i>(length=1)</i>
      1 => string &#39;y&#39; <i>(length=1)</i>
      2 => string &#39;t&#39; <i>(length=1)</i>
      3 => string &#39;h&#39; <i>(length=1)</i>
      4 => string &#39;o&#39; <i>(length=1)</i>
      5 => string &#39;n&#39; <i>(length=1)</i>
      6 => string &#39;fe&#39; <i>(length=2)</i>
  1 => 
    <b>array</b>
      0 => string &#39;P&#39; <i>(length=1)</i>
      1 => string &#39;y&#39; <i>(length=1)</i>
      2 => string &#39;t&#39; <i>(length=1)</i>
      3 => string &#39;h&#39; <i>(length=1)</i>
      4 => string &#39;o&#39; <i>(length=1)</i>
      5 => string &#39;n&#39; <i>(length=1)</i>
      6 => string &#39;fe&#39; <i>(length=2)
**/
</i></i></i>
Copy after login

Copy after login
#5 Find and Replace Actually the finditer() method in python is not a search and replace, it just returns an iterator that sequentially accesses each matching result (Match object) python:

for i in patt.finditer(strs): print i.group() #The above output ''' P y t h on fe '''

这和PHP中的preg_filter()有所不同,preg_filter()与preg_replace()都是执行一个正则表达式的搜索和替换。在python中正则方法中,用于查找替换的是:sub()与subn()。 需要注意的是sub()返回的一个新字符串,不是作用在原对象上。 subn()返回的是一个以“新字符串和替换的次数”组成的元组,也没有作用到原对象上。 #替换三次 print patt.sub('99',strs,3) #输出 '我爱99你99你99知h吗o?n哈哈fe哈' print patt.subn('99',strs) #输出:是一个元组('我爱99你99你99知99吗99?99哈哈99哈',7) 替换与引用

#这里批量替换文章中的图片的路径(old_c 是文章的内容)

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 是文章的内容)

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

<i><i><i><font><b><b><font>string &#39;我爱999你999你999知999吗999?999哈哈999哈&#39; <i>(length=51)
</i></font></b></b></font></i></i></i>
Copy after login
另注   1 对于正则的基础知识可以GOOGLE一下,Python正则的基础知识也可以GOOGLE一下。           2 对于更多关于PHP PCRE风格的正则基础,可以参看:http://cn2.php.net/manual/zh/regexp.introduction.php           3 另外有一点需要注意的是:对于处理字符串能用字符串函数处理的就用函数处理,千万别用正则。   PHP、Python 相关正则函数实例 - fc_lamp - @fc_lamp

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735170.htmlTechArticle当我们在做字符串处理时,如果字符串处理函数不能实现我们想要的时,我们就借助正则来帮助我们实现了。 一般使用正则的情况有:匹配...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

See all articles