SMARTS: distinguish between alcohol and carboxylic acid

I'm new to SMARTS and have to identify substructures with the Python package `rdkit`, so far so good. Let's say I have a SMILES with an carboxylic acid and a alcohol such as



```
OC(C1=CC(CO)=CC=C1)=O

```

and I want to get only the alcohol, then I would try something like this:



```
    mol = Chem.MolFromSmiles("OC(C1=CC(CO)=CC=C1)=O") 
    toSearch = Chem.MolFromSmarts("[#6][OX2H]")
    bool = mol.HasSubstructMatch(toSearch)
    subStruct = mol.GetSubstructMatches(toSearch)
    print(bool)
    print(subStruct)

```

which would return:
[![enter image description here](https://i.stack.imgur.com/PUAkK.png)](https://i.stack.imgur.com/PUAkK.png)



```
True
((1, 0), (5, 6))

```

However, the substructure search also identifies the OH of the acid which I don't want. How can I exclude that? My idea was to use some kind of recursive smarts for the alcohol to exclude all those "alcohols" where the carbon is bonded to another oxygen like "=O".




The simplest change to make for your SMARTS pattern would be specifying the total degree of the attached carbon is 4 with `"[#6X4][OX2H]"`. This is enough to discriminate against the carboxylic acid.


You could use the alcohol SMARTS from [here](https://github.com/openbabel/openbabel/blob/master/data/SMARTS_InteLigand.txt#L70), which has the comment



> 
> nonspecific definition, no acetals, aminals, and the like
> 
> 
> 


The SMARTS pattern is `"[OX2H][CX4;!$(C([OX2H])[O,S,#7,#15])]"`, which stipulates that the carbon attached to the oxygen not also be connected to O, S, N, or P.


I made a little [interactive widget](https://www.wolframcloud.com/obj/37fe8f9e-7c80-447c-a589-92e94d9197c4) that takes a molecule name or SMILES in the first field and a SMARTS pattern in the second.


 5