string_helper.py是字符串操作包,主要对字符串进行检查、过滤和截取等处理。

  1 #!/usr/bin/evn python
  2 # coding=utf-8
  3 
  4 import re
  5 
  6 
  7 def check_string(text, pattern):
  8     """
  9     检查字符串是否符合指定规则
 10     :param text: 需要检查的字符串
 11     :param pattern: 正式表达式,如:'^[a-zA-Z]+$'
 12     :return: 含有指定字符时返回真,否则为假
 13     """
 14     match = re.search(pattern, text)
 15     if match:
 16         return True
 17     else:
 18         return False
 19 
 20 
 21 def is_email(text):
 22     """
 23     验证字符串是否是email
 24     :param text: 需要检查的字符串
 25     :return: 符合返回True,不符合返回False
 26     """
 27     return check_string(text, '[^\._-][\w\.-]+@(?:[A-Za-z0-9]+\.)+[A-Za-z]+$')
 28 
 29 
 30 def is_phone(text):
 31     """
 32     验证字符串是否是固定电话
 33     :param text: 需要检查的字符串
 34     :return: 符合返回True,不符合返回False
 35     """
 36     return check_string(text, '\(?0\d{2,3}[) -]?\d{7,8}$')
 37 
 38 
 39 def is_mobile(text):
 40     """
 41     验证字符串是否是手机号码
 42     :param text: 需要检查的字符串
 43     :return: 符合返回True,不符合返回False
 44     """
 45     return check_string(text, '^1[3578]\d{9}$|^147\d{8}$')
 46 
 47 
 48 def is_letters(text):
 49     """
 50     验证字符串是否全是字母
 51     :param text: 需要检查的字符串
 52     :return: 符合返回True,不符合返回False
 53     """
 54     return check_string(text, '^[a-zA-Z]+$')
 55 
 56 
 57 def is_idcard(text):
 58     """
 59     验证字符串是否是身份证号码
 60     :param text: 需要检查的字符串
 61     :return: 格式正确返回True,错误返回False
 62     """
 63     ic = IdentityCard()
 64     return ic.check(text.upper())
 65 
 66 
 67 def filter_str(text, filter='\||<|>|&|%|~|\^|;|\''):
 68     """
 69     滤掉字符串
 70     :param text: 需要过滤的字符串
 71     :param filter: 过滤内容(正则表达式)
 72     :return: 去除特殊字符后的字符串
 73     """
 74     if text:
 75         return re.subn(filter, '', text)[0]
 76     else:
 77         return ''
 78 
 79 def filter_tags(htmlstr):
 80     """
 81     过滤HTML中的标签
 82     :param htmlstr: 要过滤的内容
 83     :return:
 84     """
 85     re_cdata=re.compile('//<!\[CDATA\[[^>]*//\]\]>',re.I) #匹配CDATA
 86     re_script=re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script
 87     re_style=re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style
 88     re_br=re.compile('<br\s*?/?>')#处理换行
 89     re_h=re.compile('</?\w+[^>]*>')#HTML标签
 90     re_comment=re.compile('<!--[^>]*-->')#HTML注释
 91     s=re_cdata.sub('',htmlstr)#去掉CDATA
 92     s=re_script.sub('',s) #去掉SCRIPT
 93     s=re_style.sub('',s)#去掉style
 94     s=re_br.sub('\n',s)#将br转换为换行
 95     s=re_h.sub('',s) #去掉HTML 标签
 96     s=re_comment.sub('',s)#去掉HTML注释
 97     #去掉多余的空行
 98     blank_line=re.compile('\n+')
 99     s=blank_line.sub('\n',s)
100     s=replaceCharEntity(s)#替换实体
101     return s
102 
103 def replaceCharEntity(htmlstr):
104     """
105     替换常用HTML字符
106     :param htmlstr: 要替换的字符
107     :return:
108     """
109     CHAR_ENTITIES={'nbsp':' ','160':' ',
110                 'lt':'<','60':'<',
111                 'gt':'>','62':'>',
112                 'amp':'&','38':'&',
113                 'quot':'"','34':'"',}
114     re_charEntity=re.compile(r'&#?(?P<name>\w+);')
115     sz=re_charEntity.search(htmlstr)
116     while sz:
117         entity=sz.group()#entity全称,如&gt;
118         key=sz.group('name')#去除&;后entity,如&gt;为gt
119         try:
120             htmlstr=re_charEntity.sub(CHAR_ENTITIES[key],htmlstr,1)
121             sz=re_charEntity.search(htmlstr)
122         except KeyError:
123             #以空串代替
124             htmlstr=re_charEntity.sub('',htmlstr,1)
125             sz=re_charEntity.search(htmlstr)
126     return htmlstr
127 
128 
129 def string(text, is_return_null=False):
130     """
131     sql字符串拼接专用函数
132     会在字符串两边添加'单撇号,用于生成数据库sql语句
133     :param text: 需要添加'的字符串
134     :param is_return_null: 是否返回null,是的话在字符串为空时返回null,否则返回''
135     :return:
136     """
137     if not text is None and text != '':
138         return "'" + str(text) + "'"
139     elif not is_return_null:
140         return "''"
141     else:
142         return "null"
143 
144 
145 def cut_str(text, length):
146     """
147     将字符串截取指定长度
148     :param text: 需要进行截取的字符串
149     :param length: 字符串保留的长度
150     :return:
151     """
152     if not text or not isinstance(text, str):
153         return text
154     tem = ''
155     try:
156         tem = text.decode('utf8')
157     except:
158         pass
159     if not tem or tem == '':
160         try:
161             tem = text[0:length]
162         except:
163             tem = text
164     return tem[0:length]
165 
166 
167 class IdentityCard:
168     """身份证号码验证类"""
169 
170     def __init__(self):
171         self.__Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
172         self.__Ti = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
173 
174     def calculate(self, code):
175         """计算校验位"""
176         sum = 0
177         for i in range(17):
178             sum += int(code[i]) * self.__Wi[i]
179         return self.__Ti[sum % 11]
180 
181     def check(self, code):
182         """检查输入的号码是否正确"""
183 
184         if (len(code) != 18):
185             return False
186         if self.calculate(code) != code[17]:
187             return False
188 
189         return True
View Code

相关文章:

  • 2021-11-30
  • 2021-09-07
  • 2021-09-21
  • 2021-06-10
  • 2021-09-11
  • 2021-08-03
  • 2021-06-20
  • 2021-12-31
猜你喜欢
  • 2021-05-21
  • 2021-12-29
  • 2021-10-31
  • 2022-01-07
  • 2021-06-16
  • 2021-12-17
相关资源
相似解决方案