简体   繁体   English

使用Regex从json路径中提取多个令牌

[英]Extract multiple tokens from json path using Regex

I have to extract tokens from a text which I need to match using regex. 我必须从需要使用正则表达式匹配的文本中提取令牌。 An example text would be something like this. 示例文本就是这样。

data.orderType.`order.created.time`

Right now I'm using the following regex to tokenize this string. 现在,我正在使用以下正则表达式对该字符串进行标记。

`(.*?)`|[^.]+

This regex tokenizes the string partially, and gives tokens as 此正则表达式部分标记字符串,并给出标记为

data,orderType,`order.created.time`

the problem here is when the tokens are taken backtick also gets included. 这里的问题是当令牌被提取时,反引号也被包括在内。 How can I dump the backtick and just get the following? 我该如何抛弃反引号并获得以下内容?

data,orderType,order.created.time

You already captured the part between backticks, just grab matcher.group(1) if it participated in the match (=if it matched): 您已经捕获了反引号之间的部分,如果它参与了比赛,则只需抓住matcher.group(1) (=)。

Java demo : Java演示

String s = "data.orderType.`order.created.time`";
String regex = "`([^`]*)`|[^.`]+";
List<String> result = new ArrayList<>();
Matcher m = Pattern.compile(regex).matcher(s);
while (m.find()) {
   if (m.group(1) != null) {
       result.add(m.group(1));
   } else {
       result.add(m.group());
   }
}
System.out.println(result);
// => [data, orderType, order.created.time]

Note I also added a backtick to the negated character class, [^.`]+ as I assume the backticks can only be paired. 注意,我还向否定的字符类[^.`]+添加了反引号,因为我认为反引号只能配对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM