How to get all words which starts with capital letter?

Multi tool use


How to get all words which starts with capital letter?
Given:
text <- "fsfs blabla Honda t Asus"
text <- "fsfs blabla Honda t Asus"
I want to get result:
[1] "Honda" "Asus"
[1] "Honda" "Asus"
I have done it by this function:
foo <- function(txt){
txtNew <- txt
txtNew2 <- txt
txtMemory <- ""
while(txtNew != txtMemory){
txtNew <- txtNew2
txtMemory <- txtNew2
txtNew <- gsub("\s[a-z]","",txtNew)
txtNew2 <- paste0(" ", txtNew)
}
txtNew <- sub("^\s+", "", txtNew)
strsplit(txtNew, " ")
}
foo("fsfs blabla Honda t Asus")
but I guess there is much easier way in R?
4 Answers
4
We can use str_extract
to match a capital letter ([A-Z]
) followed by a word boundary (\b
) followed by one or more word characters
str_extract
[A-Z]
\b
library(stringr)
str_extract_all(text, "\b[A-Z]\w+")[[1]]
#[1] "Honda" "Asus"
Or with gregexpr/regmatches
from base R
gregexpr/regmatches
base R
regmatches(text, gregexpr("\b[A-Z]\w+", text))
#[1] "Honda" "Asus"
In base R, you could do
grep("^[A-Z]", scan(textConnection("fsfs blabla Honda t Asus"), ""), value=TRUE)
Read 5 items
[1] "Honda" "Asus"
Here, scan
reads in the text and splits it by white space. Then grep
with values=TRUE returns all elements in the character vector which match the subexpression "^[A-Z]" which can be read "starts with a capital letter."
scan
grep
In place of scan
, you could use strsplit
/ unlist for the same result.
scan
strsplit
grep("^[A-Z]", unlist(strsplit("fsfs blabla Honda t Asus", " ")), value=TRUE)
Here's a solution without regular expressions:
text <- "fsfs blabla Honda t Asus"
x <- strsplit(text, " ", T)[[1]]
x[substr(x, 1, 1) %in% LETTERS]
# [1] "Honda" "Asus"
I would do so:
const str = "fsfs blabla Honda t Asus";
const regex = /([A-Z]w+)/g;
const result = ;
let m;
while ((m = regex.exec(str)) !== null) result.push(m[1]);
$('#result').html(JSON.stringify(result));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="result"></p>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.