JToken.SelectToken 使用正则表达式查找与模式匹配的值?
JToken.SelectToken with regex for finding values that match a pattern?
如何使用JToken.SelectToken for finding values inside a path that match a pattern (e.g. find all values that are valid email addresses)? Is there any Regex compatibility like ?
我不确定这是否在任何地方都有记录(我至少没有找到),但实际上在最新版本中(似乎从 11.0.1 开始)确实如此。语法是 =~ /regex here/
。例如:
JObject o = JObject.Parse("{\"Objects\": [{\"Email\": \"test@gmail.com\"}, {\"Email\":\"not an email\"}]}");
// returns only "test@gmail.com" token
var result = o.SelectToken(@"$.Objects[?(@.Email =~ /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)]");
如何使用JToken.SelectToken for finding values inside a path that match a pattern (e.g. find all values that are valid email addresses)? Is there any Regex compatibility like
我不确定这是否在任何地方都有记录(我至少没有找到),但实际上在最新版本中(似乎从 11.0.1 开始)确实如此。语法是 =~ /regex here/
。例如:
JObject o = JObject.Parse("{\"Objects\": [{\"Email\": \"test@gmail.com\"}, {\"Email\":\"not an email\"}]}");
// returns only "test@gmail.com" token
var result = o.SelectToken(@"$.Objects[?(@.Email =~ /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)]");