检查字符串是否包含任何特殊字符的程序
给定一个字符串,任务是检查该字符串是否包含任何特殊字符(定义的特殊字符集)。如果发现任何特殊字符,请不要接受该字符串。
例子 :
Input : Geeks$For$Geeks
Output : String is not accepted.
Input : Geeks For Geeks
Output : String is accepted
方法:将所有不需要的特殊字符创建一个正则表达式(regex)对象,然后在搜索方法中传递一个字符串。如果字符串的任何一个字符与正则表达式对象匹配,则搜索方法返回匹配对象,否则返回无。
下面是实现:
C++
// C++ program to check if a string
// contains any special character
// import required packages
#include
#include
using namespace std;
// Function checks if the string
// contains any special character
void run(string str)
{
// Make own character set
regex regx("[@_!#$%^&*()<>?/|}{~:]");
// Pass the string in regex_search
// method
if(regex_search(str, regx) == 0)
cout << "String is accepted";
else
cout << "String is not accepted.";
}
// Driver Code
int main()
{
// Enter the string
string str = "Geeks$For$Geeks";
// Calling run function
run(str);
return 0;
}
// This code is contributed by Yash_R
Python3
# Python3 program to check if a string
# contains any special character
# import required package
import re
# Function checks if the string
# contains any special character
def run(string):
# Make own character set and pass
# this as argument in compile method
regex = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
# Pass the string in search
# method of regex object.
if(regex.search(string) == None):
print("String is accepted")
else:
print("String is not accepted.")
# Driver Code
if __name__ == '__main__' :
# Enter the string
string = "Geeks$For$Geeks"
# calling run function
run(string)
PHP
?/\|}{~:]',
$string);
if($regex)
print("String is accepted");
else
print("String is not accepted.");
}
// Driver Code
// Enter the string
$string = 'Geeks$For$Geeks';
// calling run function
run($string);
// This code is contributed by Aman ojha
?>
输出 :
String is not accepted.