I want to convert all seconds contained in the XML tag to milliseconds using regex with Notepad++.
Before converting:
<keepTime>3</keepTime>
<keepTime>4.5</keepTime>
<keepTime>0.7</keepTime>
<keepTime>1.85</keepTime>
The results I want after converting:
<keepTime>3000</keepTime>
<keepTime>4500</keepTime>
<keepTime>700</keepTime>
<keepTime>1850</keepTime>
Here is the regex I use: <keepTime>([0-9]*[.])?[0-9]+</keepTime>
It matches all the values in <keepTime>
. However, I have no idea what to replace into milliseconds.
If your answer was helpful, I would appreciate it.
Thanks.
Even though I frequently use regular expressions (RE), I would be reluctant to use one complex RE for this job. The chances that it misses some of the tags, or wrongly converts others, seem too high and too risky. I would approach this sort of task using a series of simple REs that jointly give me confidence that I have done all the changes correctly.
Thus, with "Regular expression" and "Wrap around" selected:
Change <keepTime>(\d+)</keepTime>
to <done>${1}000</done>
Change <keepTime>(\d+)\.(\d)</keepTime>
to <done>${1}${2}00</done>
Change <keepTime>(\d+)\.(\d\d)</keepTime>
to <done>${1}${2}0</done>
To remove all leading zeros but retain a single zero, use
Change <done>0+([1-9]\d*)</done>
to <done>${1}</done>
For a variation on the above to remove some leading zeros but keep three digits, use:
Change <done>0+(\d\d\d)</done>
to <done>${1}</done>
It is simple to modify it to keep two digits or four, etc.
Now do a search for any remaining occurrences of </?keepTime>
and change them in the style above. Then when they are all changed, convert the "done"s with:
Change <done>(\d+)</done>
to <keepTime>\1</keepTime>
.
Now do a final check that all of the "done"s have been done, searching for </?done>
Note that done
should be replaced throughout by some tag that is not used in the original file. Note also that this is complex editing, so make a backup before doing the changes.