正则表达式查找具有空值的属性
Regex to find attributes which has null values
我正在使用 REST Assured 框架进行 API 测试,但在查找具有空值的属性时遇到了一些困难。我真的不知道正则表达式 :P
所以,API 响应就像
"data": [
{
"type": "social",
"id": "164",
"attributes": {
"created_time": "2014-09-12",
"currency": "INR",
"budget": 381000,
"end_time": null,
"name": "Untitled",
"start_time": "2022-09-12",
"updated_time": "2014-09-12"
}
我需要找到像 "end_time"
这样具有空值的属性。
如果有任何其他方法可以找到这样的属性,那将是非常有帮助的。
提前致谢!!
要在 JSON 文本中搜索 null
属性,您可以使用以下正则表达式:
/"([^"]+)": null/
以上正则表达式将捕获组 1 中所有值为 null
.
的属性
解释:
- " - 匹配引用
- ( - 捕获组开始
- [^"]+ - 将匹配(捕获)一个或多个不是引号
的字符
- ) - 捕获组结束
- " - 匹配引用
- : null - 逐字匹配冒号,然后是 space,然后是
null
以上解释翻译成简单的英语:捕获引号之间的所有字符,后跟冒号、space 和 null
。
根据您执行正则表达式所使用的语言,您需要指定全局标志以匹配所有属性。第一个匹配组通常是结果的第一个元素,是数组。
根据您的语言,可能需要或不需要正斜杠“/”- 可以将正则表达式指定为字符串,或者语言可以支持正则表达式表示法- 带有斜杠。最新的,通常通过在结束斜杠后添加g
来指定全局标志。
在Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Retrieve response from the REST API
String json_response = receiveResponse();
// Define the regex pattern
String pattern = "\"([^\"]+)" *: *null";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(json_response);
if (m.find( )) {
// Print the entire match
System.out.println("Found value: " + m.group(0) ); // > "end_time": null
// Print capture group 1
System.out.println("Found null for: " + m.group(1) ); // > end_time
} else {
System.out.println("NO MATCH");
}
您可以遍历所有匹配字段:
while (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found null for: " + m.group(1) );
}
我正在使用 REST Assured 框架进行 API 测试,但在查找具有空值的属性时遇到了一些困难。我真的不知道正则表达式 :P
所以,API 响应就像
"data": [
{
"type": "social",
"id": "164",
"attributes": {
"created_time": "2014-09-12",
"currency": "INR",
"budget": 381000,
"end_time": null,
"name": "Untitled",
"start_time": "2022-09-12",
"updated_time": "2014-09-12"
}
我需要找到像 "end_time"
这样具有空值的属性。
如果有任何其他方法可以找到这样的属性,那将是非常有帮助的。
提前致谢!!
要在 JSON 文本中搜索 null
属性,您可以使用以下正则表达式:
/"([^"]+)": null/
以上正则表达式将捕获组 1 中所有值为 null
.
解释:
- " - 匹配引用
- ( - 捕获组开始
- [^"]+ - 将匹配(捕获)一个或多个不是引号 的字符
- ) - 捕获组结束
- " - 匹配引用
- : null - 逐字匹配冒号,然后是 space,然后是
null
以上解释翻译成简单的英语:捕获引号之间的所有字符,后跟冒号、space 和 null
。
根据您执行正则表达式所使用的语言,您需要指定全局标志以匹配所有属性。第一个匹配组通常是结果的第一个元素,是数组。
根据您的语言,可能需要或不需要正斜杠“/”- 可以将正则表达式指定为字符串,或者语言可以支持正则表达式表示法- 带有斜杠。最新的,通常通过在结束斜杠后添加g
来指定全局标志。
在Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Retrieve response from the REST API
String json_response = receiveResponse();
// Define the regex pattern
String pattern = "\"([^\"]+)" *: *null";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(json_response);
if (m.find( )) {
// Print the entire match
System.out.println("Found value: " + m.group(0) ); // > "end_time": null
// Print capture group 1
System.out.println("Found null for: " + m.group(1) ); // > end_time
} else {
System.out.println("NO MATCH");
}
您可以遍历所有匹配字段:
while (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found null for: " + m.group(1) );
}