基于正则表达式将字符添加到特定位置的字符串

时间:2017-01-05 11:53:46

标签: java regex replace

有没有办法在字符串的特定位置添加一个字符,比如说String str = "key1=123 key2=text with spaces key3=foo";

例如:

this

我需要使用键值对字符串中的制表符替换键之前的所有空格。

请注意,某些值可能包含空格。

1 个答案:

答案 0 :(得分:1)

使用前瞻:

"aggs": {
"answer": {
  "terms": {
    "field": "answer.keyword"
  }
},

或类似的,抓住钥匙并把它放回去:

str = str.replaceAll(" (?=\\w+=)", "\t");