如何在 Java 中使用正则表达式拆分地图字符串
在我工作的系统中,有一些我想更改但无法更改的遗留代码。此代码将值存储在类似于以下字符串的映射中:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定这里的用例,但这里有正则表达式可以帮助您找到它
not sure of the use case here, but here are the regexes to help you find it
您是否考虑过使用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.看起来像一个 JSON 字符串...你可以使用 JSON :)
looks like a JSON string... you can use JSON :)