我正在创建一个简单的函数来检查Python中的特殊字符。我们编写了一个Python程序来检查字符串是否包含任何特殊字符。
import re
def run(string):
regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
if(regex.search(string) == None):
print("String value is accepted")
else:
print("String value is not accepted.")
if __name__ == '__main__' :
# Here in "money@rupay" the @ is a special character.
string = "money@rupay"
run(string)
# The Output is:- String value is not accepted.在函数中,我将特殊字符[@_!#$%^&*()<>?/\|}{~:] reject检查为字符串。它工作得很完美。
在python中传递特殊数字作为受限制的数字的方法是什么?我想在python中传递一些特殊的数字,比如- 9和3。如何将9 and 3设置为python程序中限制的特殊数字?
发布于 2021-01-24 14:49:00
只需在您的正则表达式模式中将9和3放入括号中:
import re
def run(string):
regex = re.compile('[@_!#$%^&*()<>?/\|}{~:93]')
if(regex.search(string) == None):
print("String value is accepted")
else:
print("String value is not accepted.") https://stackoverflow.com/questions/65867797
复制相似问题