BRZ
BRZ

Reputation: 695

Search and extract from String in R

I have a String: "Development Document ID Z585 Design No. PZ585A2202 Marked as"

I need to extract the alphanumeric part of the string: "PZ585A2202". Tried :

p_string <- "Development Document ID Z585 Design No. PZ585A2202 Marked as"
regexp <- "(([:alnum:]))"
str_extract(p_string,regexp)

But obviously,I am getting this wrong. Is there a way I can identify that alphanumeric part of the string and extract it? Unfortunately, I wont know the position or what precedes or follows it.

Upvotes: 0

Views: 231

Answers (1)

Ren&#233;R
Ren&#233;R

Reputation: 204

Supposing the string is exactly 10 characters which only include capital letters and numbers, you could do something like this:

regmatches(p_string, regexpr("([A-Z0-9]{10})", p_string))

You might get false positives if the strings include words of more than 10 letters in all caps, but unless you have more identifying features (as was mentioned in the comments) it seems unlikely you can do better.

Upvotes: 1

Related Questions