
Searching characters through a string
Peeyush Khare
Hi,
I have a 1D text wave with chemical formulas. Now I want to sort them into categories. So for example: Formulas look like CxHyNzOp where x,y,z and p are all numerals and z could also be zero. From this superset, I want to filter out a subset with the formulas of the format CxHyOp, and put them in a new 1D text wave. Kindly advise how to do this.
Many Thanks,
Peeyush
Here is what I came up with:
Adjust the Grep string 'searchFor' to match your desired chemical formula.
July 13, 2020 at 10:30 pm - Permalink
With chemical formula in proper syntax (capitalization), the search string can be shortened to this:
String searchFor = "^[C][0-9][H][0-9][O][0-9]"
July 14, 2020 at 04:40 am - Permalink
In reply to With chemical formula in… by jjweimer
"^[CHO0-9]+\\b"
this allows for longer chain hydrocarbons (ie. x, y, or p >9, otherwise in the original regex it should be 1-9 or [0-9]+), and for formulae like CH3COOH.
July 14, 2020 at 05:24 am - Permalink
In reply to "^[CHO0-9]+\\b" this allows… by tony
and before someone else points it out,
is shorter!
July 14, 2020 at 05:27 am - Permalink
These are amazing insights and they did the job! Thank you so much, chozo, jjweimer and tony! I deeply appreciate your taking out time to respond, and thoughts.
July 14, 2020 at 08:33 am - Permalink
@chozo Doesn't the Wave input need to be Wave/T?
July 14, 2020 at 10:27 am - Permalink
It was working without declaring wave/T for the input. Doesn't a function input accepts both numeric and text waves or is that considered a 'bug'?
July 14, 2020 at 03:14 pm - Permalink
Huh. I guess wfprintf is one of those baffling Igor inconsistencies. In fact, of course, wfprintf needs to work with any wave type, so it probably determines the wave's type at run-time.
July 14, 2020 at 05:00 pm - Permalink
And if you want to avoid the list conversion and just return the result as free wave:
July 15, 2020 at 02:36 am - Permalink
It's always interesting to see how a problem gets successively ground down to the bare minimum here in the forum. I was somehow under the impression that Grep is only for files. Good to learn a new trick.
July 15, 2020 at 07:47 pm - Permalink