Hi I got words that contain one of the following tokens: CO. PRO. IST.
How to clean my words from these tokens? Do you have any regex pattern?

for example the words :

convolutional NN in data science--> extract nothing   
co. Neural N --> extract co  
co NN --> extract nothing



you can use lookahead with str_extract from the stringr package.


stringr::str_extract(string = "co.asdkhsda", pattern = "co(?=\\.)") 
# extracts 'co' only when followed by a literal '.'

stringr::str_extract("convolutional NN in data science", pattern = "co(?=\\.)")
# extracts nothing, returns NA

The regex pattern


looks for 'co', followed by '\\.'

The '\\'-part is used to escape the special meaning of the '.'

The dot usually matches any character, and therefore need to be escaped.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.