如何在R中提取具有特殊字符的模式之间的字符串
How to extract string between to patterns with special characters in R
从字符串 a
我想提取 |Request|
和下一个 |
之间的所有内容:
a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
gsub(".*\|Request\| (.+) |.*", "\1", a)
以这种方式应用 gsub 没有产生预期的结果。我该怎么做呢?
你需要使用惰性点,而且你的输入模式应该匹配整个输入,因为你要用捕获组替换:
a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
sub("^.*\|Request\|\s*(.+?)\s*\|.*$", "\1", a)
[1] "Sample inlet port of the HIP cartridge with"
您可以使用 sub
捕获 |Request|
之后的所有内容,直到下一个 |
发生。
sub(".*\|Request\|(.*?)\|.*", "\1", a)
#[1] "\nSample inlet port of the HIP cartridge with "
从字符串 a
我想提取 |Request|
和下一个 |
之间的所有内容:
a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
gsub(".*\|Request\| (.+) |.*", "\1", a)
以这种方式应用 gsub 没有产生预期的结果。我该怎么做呢?
你需要使用惰性点,而且你的输入模式应该匹配整个输入,因为你要用捕获组替换:
a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
sub("^.*\|Request\|\s*(.+?)\s*\|.*$", "\1", a)
[1] "Sample inlet port of the HIP cartridge with"
您可以使用 sub
捕获 |Request|
之后的所有内容,直到下一个 |
发生。
sub(".*\|Request\|(.*?)\|.*", "\1", a)
#[1] "\nSample inlet port of the HIP cartridge with "