Question: I need to extract a number from the text as follows:

```
A<- c( '\n      0 requests\n        in 2008\n    ', '\n      1,320 requests\n        in 2008\n    ', '\n      64 requests\n        in 2008\n    ')
B<- c('John','KL', 'LK')

```

I want to get the number of requests opened from the text above. In this case I need to get the numbers 

```
A       C
John    0
KL      1,320
LK      64

```

any suggestion on how to extract this varying length substring? Thanks so much

Answer: 
Using `str_extract` and positive lookahead 

```
library(dplyr)
#\\d*\\,?\\d* (?=requests) 0+ digits followed by 0 or 1 , followed by 0+ digit 
#This pattern must followed by the word requests
df %>% mutate(C=stringr::str_extract(A,'\\d*\\,?\\d* (?=requests)'))

                                          A    B      C
1     \n      0 requests\n        in 2008\n     John     0 
2 \n      1,320 requests\n        in 2008\n       KL 1,320 
3    \n      64 requests\n        in 2008\n       LK    64 

```

Note: I assume you have maximum one `,`. In case of more than one `,` use @Hayden's suggestion `stringr::str_extract(A,'(\\d*\\,?)*\\d* (?=requests)')`
```
transform(read.table(text=gsub("\\n?\\s+",";",A),sep=";",fill = T),V1=B)[1:2]

    V1    V2
1 John     0
2   KL 1,320
3   LK    64

```