简体   繁体   English

正则表达式 Java 将 ["item1", "item2"] 替换为 item1, item2

[英]Regex Java replacing ["item1", "item2"] with item1, item2

I don't use regex that much so I am having some troubles with this right now.我不怎么使用正则表达式,所以我现在遇到了一些麻烦。 I'm trying to replace ["item1", "item2"] with item1, item2 using string.replaceAll() .我正在尝试使用string.replaceAll()item1, item2替换["item1", "item2"] However, I don't know how do I match the [ , and I don't know how to replace all special characters but the comma.但是,我不知道如何匹配[ ,也不知道如何替换除逗号之外的所有特殊字符。

You can use following regular expression to solve your problem.您可以使用以下正则表达式来解决您的问题。

String s = "[\"item1\", \"item2\"]";
System.out.println(s.replaceAll("\\[\"([a-z0-9]*)\",\\s*\"([a-z0-9]+)\"\\]", "$1, $2"));

Note that I have use parenthesis to group what I wanted and use them in the replace string.请注意,我使用括号将我想要的内容分组并在替换字符串中使用它们。 Your groups will identify as $1, $2, $3... as it appears in your regular expression.您的组将标识为 $1、$2、$3...,因为它出现在您的正则表达式中。

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

相关问题 Java正则表达式代码,用于将{Item1} .Item2处理到数组或列表中 - Java Regular Expression code to process {Item1}.Item2 into array or list 一旦用户按 2 次 y 和下一次 n,当输入 n 时如何添加 item1 和 item2 并显示总价 - once user press 2 times y and the next time n, when enter n how to add item1 and item2 and display total price Android App 向 Web App 脚本发送数据。 如何让它停止发送 Item1、Item2 等 - Android App sending data to Web App Script. How do I get it to stop sending Item1, Item2, etc 有效的Java Item1-用于创建对象的静态工厂方法 - Effective Java Item1 - Static factory method for object creation 有效的Java作者:Joshua Bloch:第1项 - 静态工厂方法 - Effective Java By Joshua Bloch: Item1 - Static Factory Method 用null(java)替换Object数组中的项目 - Replacing an item in an Object array with null (java) 用Linkedhashmap VALUE替换ArrayList项 - Replacing an ArrayList item with a Linkedhashmap VALUE 替换Jface弹出菜单中的菜单项 - Replacing menu item in Jface popupmenu Java ArrayList将当前项目添加到Previous项目; 删除当前项目 - Java ArrayList adding current item to Previous item; remove current item 用Java解析{“ item” =“ value”} - Parse {“item”=“value”} with java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM