1

How can I extract text between two strings.

For e.g.:

x <- "ABCDName:Mr.Praveen KumarDOB"

I want to extract Mr. Praveen Kumar.

Also, I want to extract string from starting till it encounters Name:.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Bitanshu Das
  • 627
  • 2
  • 8
  • 21

2 Answers2

1

Try

gsub('^[^:]+:|[A-Z]{1,}$', '', x)
#[1] "Mr.Praveen Kumar"

sub('Name.*', '', x)
#[1] "ABCD"
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You may try this,

> library(stringr)
> str_extract(x, perl("Name:\\K.*?(?=[A-Z]{2,})"))
[1] "Mr.Praveen Kumar"
> str_extract_all(x, perl("Name:\\K.*?(?=[A-Z]{2,})|.*?(?=Name:)"))[[1]]
[1] "ABCD"             "Mr.Praveen Kumar"
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Error : perl is deprecated. Please use regexp instead – Bitanshu Das Jul 20 '15 at 11:45
  • 1
    @BitanshuDas In the new stringr, there is no need to wrap with `perl`. Just use without that wrapping and it should work fine. `str_extract_all(x, "Name:\\K.*?(?=[A-Z]{2,})|.*?(?=Name:)")[[1]]` works fine for me – akrun Jul 20 '15 at 11:51