Question

I have a rather complicating data file with many rows of many different types. For the particular column I'm interested in I have a pattern that looks like this:

12.6 \pm 0.8      

^^ The number of digits before and after the decimal in each of those pieces of the entry may vary.

I'm hoping I can use regular expressions to replace that column entry to: [12.6,-0.8,+0.8]

What I am requesting help on is how I should go about replacing once I've found entries like what I had earlier. All of the examples I've found so far are for when you want to replace static strings with other static strings, but for each line I'm necessarily going to have different numbers (and different digits perhaps). The regular expression I've attempted so far to find entries like "12.6 \pm 0.8" is the following:

\d*\.\d*\s\\\w{2})\s\d*\.\d*

I would also appreciate if I could get a check on that, too. At the moment I'm just manipulating the datafile in my text editor, but I'm also open to Python solutions, too.

Thanks!

Was it helpful?

Solution

Your expression is close. Are there any conditions where this won't work?

(\d*\.\d*)\s\\\w{2}\s(\d*\.\d*)

with the replace pattern being (for JS)

[$1, -$2, $2]

or for emacs (according to http://www.emacswiki.org/emacs/RegularExpression)

[\1, -\2, \2]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top