0
$\begingroup$

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

Thanks!

$\endgroup$
0
$\begingroup$

you can use lookahead with str_extract from the stringr package.

e.g.

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

co(?=\\.) 

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.

$\endgroup$

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.