程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python regular expression

編輯:Python

Regular expression is a logical formula for string operation , It is to use some specific characters defined in advance 、 And the combination of these specific characters , Form a “ Rule string ”, To filter out the content that meets this rule .

It can be simply understood as : A powerful search tool , A regular expression is a conditional expression for what you want to search .

1. re.findall() function

effect : Traversing the entire string , You can get all the matching strings , Return a list .

General usage :

re.findall(r' Regular expressions ',' Text to match ')

2. Practice contact regularity from childhood

  1. Match words from strings to:
import re
text = "0537-146987425,0537-299656897,The moment you think about giving up,think of the reason why you held on so long. Total umbrella for someone else if he, you’re just not for him in the rain.Never put your happiness in someone else’s hands.Sometimes you have to give up on someone in order to respect yourself. aaaa bbbbcc d dddddd"
print(re.findall(r'to',text))
  • Output :
['to', 'to']
  1. Match in text China and Israel g All words at the beginning :
print(re.findall(r'\bg\w*?\b',text))
  • Output :
['giving', 'give']
  1. Find letters with length 4 's words :
print(re.findall(r'\b\w{4}\b',text))
  • Output :
['0537', '0537', 'held', 'long', 'else', 'just', 'rain', 'your', 'else', 'have', 'give', 'aaaa']
  1. Find out xxxx-xxxxxxxxx Formatted data :
print(re.findall(r'\d{4}-\d{8}',text))
  • Output :
['0537-14698742', '0537-29965689']

3. Regular expression metacharacters

Metacharacters Functional specifications ^ Matches the beginning of the string $ Matches the end of the string . Matches any character other than a newline character \d Match the Numbers \b Match the beginning or end of the word \w Match any letter 、 Numbers and underscores \s Matches any whitespace characters , Including Spaces 、 tabs 、 Page identifier \B And \b contrary , Match non word boundaries \W And \w contrary \S And \s contrary {m,n}{} The preceding character or sub pattern repeats at least m Time , at most n Time
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved