Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delimiting string in R

I hope everyone is having a blast I have come to face this challange:

I want to be able to extract one portion of a string in the folliing manner:

  1. The string may or may not have a dot or may have plenty of them
  2. I want to extract the string part that is before the first dot, if there is no dot then I want the whole string
  3. I want to use a regex to achieve this
    test<-c("This_This-This.Not This",
            "This_This-This.not_.this",
            "This_This-This",
            "this",
            "this.Not This")

since I need to use a regex, I have been trying to use this expression:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

str_match(test,"(^[a-zA-Z].+)[\\.\\b]?")[,2]

but what I get is:

> str_match(test,"(^[a-zA-Z].+)[\\.\\b]?")[,2]
[1] "This_This-This.Not This" "This_This-This.not_this"
[3] "This_This-This"          "this"                   
[5] "this.Not This"          
> 

My desired output is:

"This_This-This"
"This_This-This"
"This_This-This"
"this"
"this"

This is my thought process behind the regex

str_match(test,"(^[a-zA-Z].+)[\\.\\b]?")[,2]

(^[a-zA-Z].+)= this to capture the group before the dot since the string starts always with a letter cpas or lowers case, and all other strings after that thats why the .+

[\.\b]?=a dot or a world boundary that may or may not be thats why the ?

Is not giving what I want and I will be so happy if yo guys can help me out to understand my miskte here thank you so much!!!

>Solution :

My regex is "match anything up to either a dot or the end of the line".

library(stringr)
str_match(test, "^(.*?)(\\.|$)")[, 2]

Result:

[1] "This_This-This" "This_This-This" "This_This-This" "this" "this"          
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading