[VN] [Ren'Py] Game1 [Author1]
[VN] [Ren'Py] Game2 [Author2]
[VN] [Ren'Py] Game3 [Author3]
I have these 3 lines saved in a text file. I want to rearrange them in such a way that my output looks like:
Game1 [Author1] [VN] [Ren'Py]
Game2 [Author2] [VN] [Ren'Py]
Game3 [Author3] [VN] [Ren'Py]
When I first read the text file into RStudio, it creates a dataframe with 1 variable and the class of all the game names are as character.
a = read.csv("games.txt")
I then use grepl to exract all the names that contain [ ]
b = a$name[grepl("\\[.*", a$name)]
Here again I face an issue. I actually only want to grepl those names that start with [
b = a$name[grepl("^\\[.*", a$name)]
But using the caret does not work. Doing so gives me a dataframe of 0 columns and 3 rows
Now, b is a character of length 3 and running
b[1]
b[2]
b[3]
gives me the entire name of each game respectively
I then run strsplit to separate the words of the games by the spaces
c = strsplit(b, " +")
Now c is of class "list" of length 3
I then tried to run this code
seq = 1 : length(c)
y = NULL
for (i in seq){
tmp <- c[[i]][1]
y[i] = tmp
c[[i]][-1]
}
This ideally should have extracted [VN] from c[[1]] , stored it in y[1] and then removed [VN] from c[[1]].
However, y gets rewritten everytime and when the loop completes only c[[3]][1] is stored in y.
I am now stuck here and have very little idea about how I can proceed to get my output. Please advise
Is this what you're looking for?
Data:
x <- c("[VN] [Ren'Py] Game1 [Author1]")
Solution:
sub("(\\[[^[]+) (\\[[^[]+) (\\w+) (\\[[^[]+)", "\\3 \\1 \\2 \\4", x)
Result:
[1] "Game1 [VN] [Ren'Py] [Author1]"