使用正则表达式验证UPI ID
在这个问题中,我们将使用正则表达式来验证UPI ID。UPI是统一支付接口,分配给每个客户,其他人可以使用它向您转账。
UPI ID包含字母数字字符。
UPI ID必须包含“@”字符。
UPI ID不能包含空格。
UPI ID可以包含点(.)或连字符(-)。
问题陈述 - 我们以字符串格式给定一个UPI ID。我们需要使用正则表达式来验证UPI ID。
示例
Input: upi = "shubham@okaxis" Output: ‘Yes’
解释
UPI ID包含“@”和字母数字字符。所以它是有效的。
Input: upi = "shubhamokaxis"; Output: No
解释
UPI ID不包含“@”字符。所以它不是有效的UPI ID。
Input: upi = "shubham@:okaxis"; Output: No
解释
UPI ID包含“:”,这是一个无效字符。
正则表达式
正则表达式是一个用于识别字符串中模式的字符串。以下是验证UPI的正则表达式。
"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"
^ − 正则表达式的开头。
[0-9A-Za-z.-]{2,256} − 开头应包含2到256个字母数字、点或连字符字符。
@ − 它必须包含@字符。
[A-Za-z]{2,64} − 在“@”字符之后,它应包含2到64个字母字符。
方法一
在这种方法中,我们将使用“regex_match”方法来验证正则表达式。
算法
步骤1 - 导入“regex”库以使用与正则表达式相关的函数。
步骤2 - 使用“regex”数据类型创建一个名为“upiPatt”的正则表达式。
步骤3 - 使用empty()方法检查字符串是否为空。如果是,则返回false。
步骤4 - 使用“regex_match()”方法,并将UPI字符串作为第一个参数,正则表达式作为第二个参数。如果该方法返回true,则从函数返回true。否则,返回false。
示例
以下是各种编程语言中此操作的实现:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <regex.h>
bool validateUPI(char* upi) {
regex_t upiPatt;
int ret;
// Compile the regular expression
ret = regcomp(&upiPatt, "^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$", REG_EXTENDED);
if (ret) {
fprintf(stderr, "Could not compile regex\n");
return false;
}
// Handling the empty string
if (strlen(upi) == 0) {
return false;
}
// Matching the UPI and regular expression
ret = regexec(&upiPatt, upi, 0, NULL, 0);
regfree(&upiPatt);
if (!ret) {
return true;
} else {
return false;
}
}
int main() {
char upi[] = "shubham@okaxis";
if (validateUPI(upi)) {
printf("Yes, it is a valid UPI ID!\n");
} else {
printf("No, it is not a valid UPI ID!\n");
}
return 0;
}
输出
Yes, it is a valid UPI ID!
#include <bits/stdc++.h>
#include <regex>
using namespace std;
bool validateUPI(string upi) {
// Defining the regular expression
const regex upiPatt("^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$");
// Handling the empty string
if (upi.empty()) {
return false;
}
// Matching the UPI and regular expression
if (regex_match(upi, upiPatt)) {
return true;
} else {
return false;
}
}
int main() {
string upi = "shubham@okaxis";
if (validateUPI(upi)) {
cout << "Yes, it is a valid UPI ID!";
} else {
cout << "No, it is not a valid UPI ID!";
}
return 0;
}
输出
Yes, it is a valid UPI ID!
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static boolean validateUPI(String upi) {
Pattern upiPatt = Pattern.compile("^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$");
// Handling the empty string
if (upi.isEmpty()) {
return false;
}
// Matching the UPI and regular expression
Matcher matcher = upiPatt.matcher(upi);
if (matcher.matches()) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String upi = "shubham@okaxis";
if (validateUPI(upi)) {
System.out.println("Yes, it is a valid UPI ID!");
} else {
System.out.println("No, it is not a valid UPI ID!");
}
}
}
输出
Yes, it is a valid UPI ID!
import re
def validate_upi(upi):
upi_patt = r"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"
# Handling the empty string
if not upi:
return False
# Matching the UPI and regular expression
if re.match(upi_patt, upi):
return True
else:
return False
if __name__ == "__main__":
upi = "shubham@okaxis"
if validate_upi(upi):
print("Yes, it is a valid UPI ID!")
else:
print("No, it is not a valid UPI ID!")
输出
Yes, it is a valid UPI ID!
时间复杂度 – O(N) 用于匹配模式。
空间复杂度 – O(1),因为我们不使用任何动态空间。
方法二
在这种方法中,我们将使用regex_search()方法使用正则表达式验证UPI ID。
算法
步骤1 - 使用“regex”数据类型创建正则表达式。
步骤2 - 如果字符串为空,则返回false。
步骤3 - 将UPI和正则表达式作为regex_search()方法的参数来验证UPI ID。
步骤4 - 根据regex_search()方法的返回值返回true或false。
示例
以下是上述方法的程序
#include <stdio.h>
#include <stdbool.h>
#include <regex.h>
bool checkForUPI(char* upi) {
regex_t upiRegex;
// Defining the regular expression
if (regcomp(&upiRegex, "^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$", REG_EXTENDED) != 0) {
return false;
}
// For empty strings
if (upi[0] == '\0') {
return false;
}
// Using the regexec() method
if (regexec(&upiRegex, upi, 0, NULL, 0) == 0) {
regfree(&upiRegex);
return true;
} else {
regfree(&upiRegex);
return false;
}
}
int main() {
char upi[] = "abcd@oksbi";
if (checkForUPI(upi)) {
printf("Yes, it is a valid UPI ID!\n");
} else {
printf("No, it is not a valid UPI ID!\n");
}
return 0;
}
输出
Yes, it is a valid UPI ID!
#include <bits/stdc++.h>
#include <regex>
using namespace std;
bool checkforUPI(string upi) {
// Defining the regular expression
const regex upiPatt("^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$");
// For empty strings
if (upi == "") {
return false;
}
// Using the regex_search() method
if (regex_search(upi, upiPatt)) {
return true;
} else {
return false;
}
}
int main() {
string upi = "abcd@oksbi";
if (checkforUPI(upi)) {
cout << "Yes, it is a valid UPI ID!";
} else {
cout << "No, it is not a valid UPI ID!";
}
return 0;
}
输出
Yes, it is a valid UPI ID!
import java.util.regex.*;
public class Main {
public static boolean checkForUPI(String upi) {
// Defining the regular expression
String upiPattern = "^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$";
// For empty strings
if (upi.isEmpty()) {
return false;
}
// Using the Pattern and Matcher classes
Pattern pattern = Pattern.compile(upiPattern);
Matcher matcher = pattern.matcher(upi);
if (matcher.find()) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String upi = "abcd@oksbi";
if (checkForUPI(upi)) {
System.out.println("Yes, it is a valid UPI ID!");
} else {
System.out.println("No, it is not a valid UPI ID!");
}
}
}
输出
Yes, it is a valid UPI ID!
import re
def check_for_upi(upi):
# Defining the regular expression
upi_pattern = r"^[0-9A-Za-z.-]{2,256}@[A-Za-z]{2,64}$"
# For empty strings
if not upi:
return False
# Using the regex.search() method
if re.search(upi_pattern, upi):
return True
else:
return False
if __name__ == "__main__":
upi = "abcd@oksbi"
if check_for_upi(upi):
print("Yes, it is a valid UPI ID!")
else:
print("No, it is not a valid UPI ID!")
输出
Yes, it is a valid UPI ID!
时间复杂度 – O(N) 用于验证UPI ID。
空间复杂度 – O(1)
regex_search()和regex_match()方法用于使用正则表达式验证字符串,但它们在输入字符串与模式的匹配行为方面存在一些细微差别。程序员可以使用任何方法来使用正则表达式验证UPI ID。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP