简体   繁体   中英

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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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