Вы можете использовать
df = data.frame(Text=c(("in which some columns are 1A265T , SDFG123 up for some rows."),
("It's too large to 12345AB MB eyeball in order to identify D12345AB"),
("some data to the axis A6651F correct columns for these rows"),
("Any output that would allow me to identify that AJ_DF125AA12."),
("how do I find some locations 564789.")))
df$Pattern <- lapply(str_extract_all(df$Text, "\\b[A-CE-Z0-9][A-Z0-9]{4,8}\\b"), paste, collapse=",")
df[df==''] <- NA
Выход:
Text Pattern
1 in which some columns are 1A265T , SDFG123 up for some rows. 1A265T,SDFG123
2 It's too large to 12345AB MB eyeball in order to identify D12345AB 12345AB
3 some data to the axis A6651F correct columns for these rows A6651F
4 Any output that would allow me to identify that AJ_DF125AA12. NA
5 how do I find some locations 564789. 564789
Регулярное выражение соответствует
\b
- граница слова [A-CE-Z0-9]
- цифры ASCII или заглавные буквы, отличные от D
[A-Z0-9]{4,8}
- от четырех до восьми цифр ASCII или заглавные буквы \b
- граница слова.
См. Демонстрационную версию regex .
Обратите внимание, что вы можете "упростить" шаблон с помощью негативного взгляда:
\b(?!D)[A-Z0-9]{5,9}\b
См. это регулярное выражение , где (?!D)
требует, чтобы следующий символ не был D
.