4

I wish to extract all unique substrings of text from a text file using R, that adhere to the form "matrixname[rowname,column number]". I have achieved only limited success with grep and extract_string_all (stringr) in the sense that it will only return the entire line and not the substring only. Trying to replace the unwanted text using gsub has been unsuccessful. Here is an example of the code that I have been using.

#Read in file
txt<-read.table("Project_R_code.R")
#create new object to create lines that contain this pattern    
txt2<-grep("param\\[.*1\\]",txt$V1, value=TRUE)
#remove all text that does not match the above pattern
gsub("[^param\\[.*1\\]]","", txt2,perl=TRUE)

The second line works (but again doesn't give me a substring of that pattern only). However the gsub code for removing non-matching patterns keeps the lines and turns them into something like this:

[200] "[p.p]param[ama1]param[ama11]*[r1]param[ama1]...

and I have no idea why. I realise this method of paring down the line into something more manageable is more tedious but it's the only way I know how to get the patterns.

Preferably I would prefer R to spit out a list of all the (unique) substrings it finds in the text file, that match my pattern, but I don't know the command. Any help on this is much appreciated.

4
  • strapplyc in the gsubfn can be used to extract text using regular expressions. strapply in the same package can extract and transform at the same time. Jun 11, 2014 at 11:44
  • 2
    Next time please make your example minimal and self contained. Jun 11, 2014 at 11:45
  • Can you provide some sample input and desired output? I'm very confused as to what you want to do. It seems like regexpr() and regmatches() would do the trick but I can't test with the information you provided.
    – MrFlick
    Jun 11, 2014 at 12:55
  • @MrFlick Sorry for the late reply. Here is a sample of input that I am using. GS["id_y", 1] = param["id_alpha", 1] * (GS["id_x1", 1] + (1 - param["id_beta", 1]) * GS["id_x2", 1]) There are multiple lines in the text file similar to this. Desired output is a list consisting of param["id_alpha", 1],param["id_beta", 1] and all other such matrices in the text file. Hope this helps you and apologies for the way this looks. Jun 16, 2014 at 9:31

1 Answer 1

1

If you'd like to extract individual components, try str_match:

test <- c("aaa[name1,1]", "bbb[name2,3]", "ccc[name3,3]")
stringr::str_match(test, "([a-zA-Z0-9_]+)[[]([a-zA-Z0-9_]+),.*?(\\d+)\\]")
##      [,1]           [,2]  [,3]    [,4]
## [1,] "aaa[name1,1]" "aaa" "name1" "1" 
## [2,] "bbb[name2,3]" "bbb" "name2" "3" 
## [3,] "ccc[name3,3]" "ccc" "name3" "3" 

Otherwise, use str_extract.

Note that to match [ in ERE/TRE we use a set containing a single [ character, i.e. [[].

Moreover, if you have many matches in a single string, use str_match_all or str_extract_all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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