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

Python regular expression

編輯:Python
# coding:utf-8
# Regular expressions
# Special characters in regular expressions
# Special characters describe
# \d Match any decimal number , And [0-9] Agreement
# \D Match any non number
# \w Match any alphanumeric underscores and unicode Character set
# \W Matches non alphanumeric data and underscores
# \s Match any space character , And [\n \t \r \v \f] identical ; \r It's return ; \n It's line breaking ; Vertical tabs (\v)、 Page identifier (\f)
# \S Match any non null character
# \A The start of the matching string
# \Z Matches the end of the string
# . Match any character ( except \n outside ); Also called wildcards
import re
data = "My name is Neo, I'm 30 years old."
result_int = re.findall("\d", data)
print(' Matching decimal digits :', result_int)
result_D = re.findall('\D', data)
print(' Match any non number :', result_D)
result_w = re.findall('\w', data)
print(' Match any number , Letter , Underline ,unicode Character set :', result_w)
result_W = re.findall('\W', data)
print(' Match any non number , Letter , Underline ,unicode Character set :', result_W)
result_s = re.findall('\s', data)
print(' Match space characters :', result_s)
result_S = re.findall('\S', data)
print(' Match non empty characters :', result_S)
result_A = re.findall('\AM', data)
print(' Match with My Starting string :', result_A)
result_Z = re.findall('old.\Z', data)
print(' Match with old. a null-terminated string :', result_Z)
# Define a function to determine whether the passed in parameter contains a number
def has_number(num):
result = re.findall('\d', num)
# if len(result) > 0:
# return True
# else:
# return False
for i in result:
return True
return False
# Define a function to determine whether the passed in parameter has a number , Delete if there is one
def remove_num(data):
res = re.findall('\D', data)
print(res)
return ''.join(res)
# Defined function start_with Determine whether the incoming data starts with a string
def start_with(sub, data):
_sub = "\A{}".format(sub)
result = re.findall(_sub, data)
print(result)
if len(result) > 0:
return True
else:
return False
# Defined function end_with Determine whether the data ends in a string
def end_with(sub, data):
_sub = "{}\Z".format(sub)
result = re.findall(_sub, data)
if len(result) > 0:
return True
else:
return False
# Defined function real_len Determine the true length of the string , Except for the space
def real_len(data):
res = re.findall('\S', data)
return len(res)
if __name__ == '__main__':
has_num = has_number('322ssssfdfd')
print(has_num)
rev = remove_num('326f5656, fdsfsd')
print(rev)
a = start_with('my', 'my myname is Liuminda ')
print(a)
e = end_with('my', 'my myname is Liuminda ')
print(e)
l = real_len('abc def ljl ')
print(l)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved