2012-05-15 39 views
-4

我與語法如何使用正則表達式替換編碼字符串中的字符?

"encodedProp:encodedValue OPERATOR encodedProp1:encodedValue1" 

(操作者可能是ANDORNOT,並且有N個對prop:value)編碼的字符串。

"encodedProp","encodedValue","encodedProp1","encodedValue1" ...是編碼的字符串。

我想用正則表達式替換":"" = "。此外,":"左側的部分應替換爲"\"" + left_part + "\"",右側部分應替換爲"'" + right_part + "'"

有了上面的例子中,替換後的字符串應該是:

"\"encodedProp\" = 'encodedValue' OPERATOR \"encodedProp1\" = 'encodedValue1'" 

什麼是我必須使用表達式來做到這一點?

+1

你有問題嗎? – jtahlborn

+0

當然,你可以更好地格式化你的問題?也很難真正告訴你想要什麼。 –

+2

不要只是要求我們爲你寫代碼。向我們展示您已有的相關代碼,並詢問您需要幫助的具體問題。 –

回答

0

好的,我在這邊出去是因爲問題沒有明確定義,但讓我們試一試。

String resultString = subjectString.replaceAll(
    "(?x)(  # Match and capture in backreference number 1:\n" + 
    " [^\\s:]+ # one or more characters except spaces or colons\n" + 
    ")   # End of capturing group 1\n" + 
    ":   # Match a colon\n" + 
    "(   # Match and capture in backreference number 2:\n" + 
    " [^\\s:]+ # one or more characters except spaces or colons\n" + 
    ")   # End of capturing group 2", 
    "\"$1\" = '$2'"); 

這完全忽略了OPERATOR一部分 - 它只是查找包含一個冒號字符序列,幷包裝它們的單/雙引號,取代結腸沿途。