'Remove a pattern from a character
I have a character like this: x = "abc [File: abcbdosln}} \n abc \n"
And I want to remove File: abcbdosln}} \n
from x
so that the result would be "abc [ abc \n"
.
I tried the gsub
function but have not yet obtained the correct result.
gsub("[File].*[\n]", "", x) # this one would remove all the content of x
gsub("File.*\n", "", x, fixed = TRUE) # this one does not work
I am trying to solve this problem in a general sense, that is, how to remove this pattern through regular expression.
Solution 1:[1]
gsub
with fixed = TRUE
flag means that you want a literal string evaluation, not a regular expression.
Remove the flag, use non-greedy match and it will work.
Input:
x <- "abc [File: abcbdosln}} \n abc \n"
gsub("File.+?\\n", "", x)
Output:
"abc [ abc \n"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |