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
  • 46,390
  • 10
  • 104
  • 180
Bitanshu Das
  • 577
  • 2
  • 7
  • 20

2 Answers2

1

Try

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

sub('Name.*', '', x)
#[1] "ABCD"
akrun
  • 789,025
  • 32
  • 460
  • 575
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
  • 166,785
  • 24
  • 204
  • 249
  • 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