-2

From string a I would like to extract everything between |Request| and the next occurance of |:

 a <- "|Request|\nSample inlet port of the HIP cartridge with |overflow| formed "
    
 gsub(".*\\|Request\\| (.+) |.*", "\\1", a)

Applying gsub this way did not yield the expected result. How could I do it instead?

volfi
  • 435
  • 3
  • 11

2 Answers2

1

You need to use lazy dot, and also your input pattern should match the entire input, given that you are replacing with capture group:

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"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • If the |-symbol does not occurr afterwards anymore I would like to get the substring from the first match until the end. How could I do this? – volfi Aug 17 '20 at 09:25
  • I don't have enough sample inputs to come up with a good enough alternative regex even worthy of a comment here. – Tim Biegeleisen Aug 17 '20 at 10:01
1

You can use sub to capture everything after |Request| until the next | occurs.

sub(".*\\|Request\\|(.*?)\\|.*", "\\1", a)
#[1] "\nSample inlet port of the HIP cartridge with "
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213