|
This post has NOT been accepted by the mailing list yet.
Hi,
how do I make this character: "Lab.01.Entrance..1999102." look like this "Lab 01 Entrance (1999102)" ?? I thought about strsplit() and then a bunch of paste statements, but there has to be an easier way. Thanks for any ideas. -Chuck |
|
This post has NOT been accepted by the mailing list yet.
HI,
Try this: new1<-"Lab.01.Entrance..1999102." new2<-gsub("\\."," ",new1) gsub("(.*\\s.*\\s.*)\\s+(.*)\\s","\\1(\\2)",new2) #[1] "Lab 01 Entrance (1999102)" #or gsub("(.*Entrance{0,1})\\s+(.*)\\s","\\1 (\\2)",new2) #[1] "Lab 01 Entrance (1999102)" A.K. |
|
This post has NOT been accepted by the mailing list yet.
In reply to this post by chuck.01
Hi,
Another one step approach: new1<-"Lab.01.Entrance..1999102." gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1 \\2 \\3 (\\4)",new1) #[1] "Lab 01 Entrance (1999102)" A.K. |
|
This post has NOT been accepted by the mailing list yet.
Thank you greatly Arun.
Could you please tell me what this is generally called: \.(.*)\\.(.*)\\.\\. it makes no sense, and I would like to find some documentation so I can understand it. Thanks so much!
|
|
This post has NOT been accepted by the mailing list yet.
are these "regular expressions" used in the grep() and gsub() functions?
|
|
This post has NOT been accepted by the mailing list yet.
HI,
Yes, these are regular expressions. #To understand it, may be it is easier to split the whole gsub() #Here, the expression inside the first quotes look for the pattern to match; the expression inside the second quote is #the replacement. #For example, if I want to return only "Lab" or "01" or "Entrance" gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1",new1) #[1] "Lab" gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\2",new1) #[1] "01" gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\3",new1) #[1] "Entrance" This can be also achieved with a simple expression gsub("(Lab{0,1}).*","\\1",new1) #[1] "Lab" #In the whole expression gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1 \\2 \\3 (\\4)",new1) new1<-"Lab.01.Entrance..1999102." "\\1" (first parenthesis from left) will return "Lab", escaped "." with "\\." ".*" means zero or more of any character. If you follow the logic, dots are replaced with spaces inside the second " ". Hope it helps. A.K. |
|
This post has NOT been accepted by the mailing list yet.
In reply to this post by chuck.01
Hi,
If I change the string a little bit, and if I want to get the same result or slightly different results: new2<- "Lab.01_Entrance._1946." gsub("(.*)\\.(.*)\\_(.*)\\.\\_(.*)\\.","\\1 \\2 \\3 (\\4)",new2) #[1] "Lab 01 Entrance (1946)" gsub("(.*)\\.(.*)\\_(.*)\\.\\_(.*)\\.","\\1 (\\2) \\3 (\\4)",new2) #[1] "Lab (01) Entrance (1946)" A.K. |
|
This post has NOT been accepted by the mailing list yet.
You are awesome; Thanks for this great starting point. I will take it from here.
Cheers!
|
| Powered by Nabble | Edit this page |
