Question: In R, I would like to use a `grep` or 'grepl' or 'gsub' command to find all elements in the vector of strings, that have either an A road or M road or a B road name in it..

Please see example below

```
tmp <- c('Little Street','A323', 'Essex Road (A43)', 'M43','Orange street','M4','B2045','New Street')

```

And I would like a function to return...

```
c('Minor Road','A Road', 'A Road', 'M Road', 'Minor Road', 'M Road','B Road','Minor Road')

```

My first thought was to use something like 

```
grepl('[0-9]',tmp)

```

but this can't distinguish between the A road, B road and M road....

As always any help would be greatly be appreciated...

Answer: 
How about this

```
tmp <- c('Little Street','A323', 'Essex Road (A43)', 'M43','Orange street','M4','B2045','New Street')

road <- rep("Minor", length(tmp))
m <- regexpr("\\b[ABM]\\d+", tmp)
road[m!=-1] <- substr(regmatches(tmp, m),1,1)
paste(road, "Road")

```

We use `regmatches()` and `regexpr()` to find and extract A,B, or M followed by one more more letters
You could break it down into steps using `grepl` and `sub` ...

```
> tmp[!grepl('[ABM]\\d', tmp)] <- 'Minor Road'
> sub('.*([ABM])\\d.*', '\\1 Road', tmp)
# [1] "Minor Road" "A Road"     "A Road"     "M Road"     "Minor Road"
# [6] "M Road"     "B Road"     "Minor Road"

```