由于不需要的分隔符,字符串拆分失败

时间:2022-02-06 02:42:39

This is the string I need to split for putting in map as key-val pair:

这是我需要拆分的字符串,用于将map作为key-val对放入:

"jti":"4ef61081-e2e0-40e4-a9ad-8f2bf33f8923","exp":1525357546,"nbf":0,"iat":1525271146,"iss":"https://dev.open-sunbird.org/auth/realms/sunbird","aud":"admin-cli"

I tried with

我试过了

String[] parts = body.split(":|,");

String [] parts = body.split(“:|,”);

Problem with this approach is the ":" in https link. See the output as follows

这种方法的问题是https链接中的“:”。请参阅输出如下

--"jti"--"4ef61081-e2e0-40e4-a9ad-8f2bf33f8923"
--"exp"--1525357546
--"nbf"--0
--"iat"--1525271146
--"iss"--"https
--//dev.open-sunbird.org/auth/realms/sunbird"--"aud"

Any lead for the exact regex to solve the issue will be appreciated. (On top of my head is if we can do a check that every spitted word either starts and ends with " or doesn't start and end with ". But I feel that is a naive approach. even if we can do it.)

任何领导正确的正则表达式来解决问题将不胜感激。 (最重要的是,如果我们能够检查每个吐出的单词是以“开始还是结束”开头和结尾。但我觉得这是一种天真的方法。即使我们能做到这一点。)

1 个解决方案

#1


2  

No need to get fancy with regex. There are a couple options.

没有必要使用正则表达式。有几种选择。

  1. This is clearly claims / attributes on a JWT token. Use a library to parse the JWT instead of parsing the string this way.
  2. 这显然是JWT令牌上的声明/属性。使用库来解析JWT而不是以这种方式解析字符串。

  3. Just split first by commas, and then by the FIRST colon. Should give you what you want without trying to respect the position of the quotes.
  4. 首先用逗号分隔,然后用FIRST冒号分割。应该给你你想要的,而不是试图尊重报价的位置。

  5. It's JSON, so use a JSON parser.
  6. 它是JSON,所以使用JSON解析器。

#1


2  

No need to get fancy with regex. There are a couple options.

没有必要使用正则表达式。有几种选择。

  1. This is clearly claims / attributes on a JWT token. Use a library to parse the JWT instead of parsing the string this way.
  2. 这显然是JWT令牌上的声明/属性。使用库来解析JWT而不是以这种方式解析字符串。

  3. Just split first by commas, and then by the FIRST colon. Should give you what you want without trying to respect the position of the quotes.
  4. 首先用逗号分隔,然后用FIRST冒号分割。应该给你你想要的,而不是试图尊重报价的位置。

  5. It's JSON, so use a JSON parser.
  6. 它是JSON,所以使用JSON解析器。