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

Extract multiple matches using a pattern from a String

I want to extract some substrings from a bigger string using some pattern. Below is my code:

library(stringr)
str_extract_all("'abcd:3343', sdgshdg374 'rgjrkgj4252:sfsfd', wdwdw'wdwd:364:ssfd', 3434", "[''][a-zA-Z0-9]['']")

Basically, I want to match pattern like ['][anything with any length]['] and followed by ,[space]. Therefore I was expecting I should get matches as 'abcd:3343' & 'rgjrkgj4252:sfsfd' & 'wdwd:364:ssfd'. Notice that for each above cases, all substrings are followed by ,[space]

With my above code, I am getting no match.

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

Can you please help how to construct right pattern match for this problem?

>Solution :

We may need to add : also

library(stringr)
str_extract_all("'abcd:3343', sdgshdg374 'rgjrkgj4252:sfsfd', 
       wdwdw'wdwd:364:ssfd', 3434", "'[A-Za-z0-9:# ]+'")[[1]]

-output

[1] "'abcd:3343'"         "'rgjrkgj4252:sfsfd'" "'wdwd:364:ssfd'"    

Or it could be also to match the ' followed by one or more characters that are not ' ([^']+) and the '

str_extract_all("'abcd:3343', sdgshdg374 'rgjrkgj4252:sfsfd', 
       wdwdw'wdwd:364:ssfd', 3434", "'[^']+'")[[1]]
[1] "'abcd:3343'"         "'rgjrkgj4252:sfsfd'" "'wdwd:364:ssfd'"    
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