如何在 Java 中使用正则表达式拆分地图字符串

发布于 2024-10-10 03:38:28 字数 380 浏览 0 评论 0原文

在我工作的系统中,有一些我想更改但无法更改的遗留代码。此代码将值存储在类似于以下字符串的映射中:

userId: "929290"; name: "Donnie Darko"; obj : {field1: "field"; field2: "field2"} phone: "666-6666";

请注意,对象映射后面没有分号,但每个其他键/值对都有分号。有没有办法在Java中使用Regex并获取该映射的第一级,这样我就可以:

userId: "929290"
obj : {field1: "field"; field2: "field2"} 

我只想要第一级,我不想单独解析field1和field2。

In the system I work on there is some legacy code that I would love to change, but can't. This code is storing values in a map which looks like the following string:

userId: "929290"; name: "Donnie Darko"; obj : {field1: "field"; field2: "field2"} phone: "666-6666";

Notice that the object map isn't followed by a semicolon, but every other key/value pair is. Is there a way to use Regex in Java and get the first level of this map, so that I could have:

userId: "929290"
obj : {field1: "field"; field2: "field2"} 

I only want the first level, I'm not looking to parse out field1 and field2 individually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千鲤 2024-10-17 03:38:28

不确定这里的用例,但这里有正则表达式可以帮助您找到它

 Pattern userIdPattern = Pattern.compile("^userId:\\s*\"(\\d+)\";.*$"); // will be the userId number
 Pattern objPattern = Pattern.compile(".*(obj\\s*:\\s*\{[^\}]+\}).*"); //will be the JSON object inside
 Matcher userIdMatcher = userIdPattern.matcher("userId: \"929290\"; name: \"Donnie Darko\"; obj : {field1: \"field\"; field2: \"field2\"} phone: \"666-6666\";");
 if(userIdMatcher.find()){
     System.out.println("userId : " + .group(1));
 }
 Matcher objPatternMatcher = objPattern.matcher("userId: \"929290\"; name: \"Donnie Darko\"; obj : {field1: \"field\"; field2: \"field2\"} phone: \"666-6666\";");
 if(objPatternMatcher.find()){
     System.out.println(objPatternMatcher.group(1));
 }

not sure of the use case here, but here are the regexes to help you find it

 Pattern userIdPattern = Pattern.compile("^userId:\\s*\"(\\d+)\";.*$"); // will be the userId number
 Pattern objPattern = Pattern.compile(".*(obj\\s*:\\s*\{[^\}]+\}).*"); //will be the JSON object inside
 Matcher userIdMatcher = userIdPattern.matcher("userId: \"929290\"; name: \"Donnie Darko\"; obj : {field1: \"field\"; field2: \"field2\"} phone: \"666-6666\";");
 if(userIdMatcher.find()){
     System.out.println("userId : " + .group(1));
 }
 Matcher objPatternMatcher = objPattern.matcher("userId: \"929290\"; name: \"Donnie Darko\"; obj : {field1: \"field\"; field2: \"field2\"} phone: \"666-6666\";");
 if(objPatternMatcher.find()){
     System.out.println(objPatternMatcher.group(1));
 }
庆幸我还是我 2024-10-17 03:38:28

您是否考虑过使用ANTLR?它是一种比正则表达式更强大的语言识别器。这样,您就可以处理复合字段(例如 {field1: {field3:"field3"; field4="field4"}; field2: "field2"})。学习曲线比不过学习正则表达式;但是,在我看来,学习 ANTLR 是值得的。

Have you thought of using ANTLR? It is a language recognizer which is much more powerful than a regex. That way, you could deal with composite fields (e.g. {field1: {field3:"field3"; field4="field4"}; field2: "field2"}) The learning curve is steeper than that of learning regexes though; but, in my opinion, learning ANTLR is worth it.

拧巴小姐 2024-10-17 03:38:28

看起来像一个 JSON 字符串...你可以使用 JSON :)

looks like a JSON string... you can use JSON :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文