I have an ascii file of the form:
04/05/2008|00:00|17|8|0|13
95|243|641
|1|0|21|0|
14998|0|0|
0|0|0|0|0|
0|0|0|0|18
00|4|0|0|
04/05/2008|00:00|22|15|0|7
64|0|238|0
|0|0|0|149
98|0|0|0|0
|0|0|0|0|0
|0|0|1801|
4|86|0|
04/05/2008|00:00|20|13|0|1
487|98|149
|1341|0|52
|0|14993|0
|0|0|0|0|0
|0|0|0|0|3
6|1800|4|1
25|4|
The dates (04/05/2008) don't necessarily always occur at the start and can occur multiple times in each line - there can be a lot of lines.
I want to read this file in, change the date format to yyyy/mm/dd and resave the file.
String dateRegEx = '([0-9]{2})/([0-9]{2})/([0
-9]{4})'
def myFile = new File(myfile.txt)
def matcher = (myFile.text =~ dateRegEx)
Now I have been having trouble with the replace...I'm thinking of something like:
myFile.text.eachMatch(date
RegEx) {
def year = matcher[numMatch][3] // println "year = $matcher[0][3]" does not work
def month = matcher[numMatch][1]
def day = matcher[numMatch][2]
String newDate = "$year/$month/$day"
//println newDate
numMatch++
// no replace, how can do this with replaceAll?
// myFile.text.replace(dateRe
gEx,newDat
e)
}
Any advice appreciated,
Thanks, Tom.
Start Free Trial