问题描述
我正在创建一个简单的函数来检查 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.
在函数中,我检查特殊字符 [@_!#$%^&*()<>?/\|}{~:]
拒绝为字符串。
它完美运行。
在python中将特殊数字作为限制数字传递的方法是什么?
我想在 python 中传递一些数字作为特殊数字 - 9
和 3
。如何将 9 and 3
设为在 python 程序中受限的特殊数字?
解决方法
只需将 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.")