(मामले में पोस्टिंग किसी के पास इसका उपयोग हो सकता है)।
मैं ओपी की तुलना में थोड़ा अधिक परिष्कृत समस्या के समाधान की तलाश कर रहा था - हर चीज की संख्या के साथ घटित संख्या के साथ एक ही घटना की जगह
उदाहरण के लिए कुछ इस तरह से:
<row id="1" />
<row id="2" />
<row id="1" />
<row id="3" />
<row id="1" />
इसके द्वारा:
<row id="2" />
<row id="3" />
<row id="2" />
<row id="4" />
<row id="2" />
Cannt ऑनलाइन समाधान नहीं मिल रहा है इसलिए मैंने अपनी स्क्रिप्ट ग्रूवी में लिखी (थोड़ा बदसूरत लेकिन काम करता है):
def replace_inc(String text, int startingIndex, String findTemplate) {
assert findTemplate.contains('_') : 'search template needs to contain "_" placeholder'
assert !(findTemplate.replaceFirst('_','').contains('_')) : 'only one "_" placeholder is allowed'
assert !text.contains('_____') : 'input text should not contain "______" (5 underscores)'
while (true) {
findString = findTemplate.replace("_",(startingIndex).toString())
if (!text.contains(findString)) break;
replaceString = findTemplate.replace("_", "_____"+(++startingIndex).toString())
text = text.replaceAll(findString, replaceString)
}
return text.replaceAll("_____","")
}
findTemplate = 'Row="_"'
path = /C:\TEMP\working_copy.txt/
startingIndex = 0
f = new File(path)
outText = replace_inc(f.text,startingIndex,findTemplate)
println "Results \n: " + outText
f.withWriter { out -> out.println outText }
println "Results written to $f"