返回的变量中校验组成其中51040305-5是变化的其他的是固定的
时间: 2024-03-26 13:41:41 浏览: 8
如果返回的变量中校验组成的字符串中有一部分是固定的,而另一部分是变化的,那么您可以使用Java中的正则表达式来匹配这个字符串。具体实现如下:
```java
String result = "Duplicate entry '51040305-5' for key 'PRIMARY'";
String pattern = "Duplicate entry '(\\d+)-\\d+' for key 'PRIMARY'";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(result);
if (m.find()) {
String target = m.group(1);
System.out.println("返回的变量中包含" + target);
} else {
System.out.println("返回的变量中不包含指定字符串");
}
```
其中,pattern是一个正则表达式,用来匹配返回的字符串中符合要求的部分,"\\d+"表示匹配一个或多个数字字符。Matcher的find()方法会返回是否匹配到了指定的字符串,group(1)方法会返回第一个匹配到的括号内的子串,也就是变化的部分"51040305"。如果匹配到了指定的字符串,则会输出"返回的变量中包含51040305",否则会输出"返回的变量中不包含指定字符串"。
相关问题
Java校验返回的一个变量中是否包含 Dupliacte entry '51040305-5' for key 'PRIMARY' 其中 51040305-5是变量
您可以使用Java中的字符串方法contains()来判断返回的变量中是否包含指定的字符串"Duplicate entry '51040305-5' for key 'PRIMARY'",如下所示:
```java
String result = "Duplicate entry '51040305-5' for key 'PRIMARY'";
String target = "51040305-5";
if (result.contains(target)) {
System.out.println("返回的变量中包含" + target);
} else {
System.out.println("返回的变量中不包含" + target);
}
```
其中,result是返回的变量,target是要查找的字符串"51040305-5"。如果返回的变量中包含指定的字符串,则会输出"返回的变量中包含51040305-5",否则会输出"返回的变量中不包含51040305-5"。
用正则快校验除了51040305-5之外的部分是否是固定的
如果您想快速校验返回的变量中除了"51040305-5"之外的部分是否是固定的,可以使用Java中的正则表达式来匹配。具体实现如下:
```java
String result = "Duplicate entry '51040305-5' for key 'PRIMARY'";
String pattern = "Duplicate entry '([^-]+)-5' for key 'PRIMARY'";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(result);
if (m.find()) {
String target = m.group(1);
System.out.println("返回的变量中除了51040305-5之外的部分是固定的:" + target);
} else {
System.out.println("返回的变量中不符合要求");
}
```
其中,pattern是一个正则表达式,用来匹配返回的字符串中符合要求的部分。"[^-]+"表示匹配一个或多个非"-"字符,也就是除了"51040305-5"之外的部分。Matcher的find()方法会返回是否匹配到了指定的字符串,group(1)方法会返回第一个匹配到的括号内的子串,也就是除了"51040305-5"之外的固定部分。如果匹配到了指定的字符串,则会输出"返回的变量中除了51040305-5之外的部分是固定的:",后面跟上固定的部分,否则会输出"返回的变量中不符合要求"。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)