-
Notifications
You must be signed in to change notification settings - Fork 20
Question about regular expression and loop #535
Comments
Hey @chenchenguo, Can you provide us with a bit more context (what is the input, output, and what did you want to accomplish)? |
Thanks @ChadFibke The data is all those words filtered from words.txt, which has same starting and ending letter like "bob", "kick". |
words <- readLines("words.txt") output <- vector("character", length(letters)) This gives the regex df <- tibble(letters, for (i in output) { frequency table I think should work |
Ah I found something as well:
|
|
Also.. I converted all the string to lowercase using:
If you do not want to count, and actually want to see the words remove then replace:
|
Here's another possibility... There's an exercise from Hadley's R for Data Science in the strings chapter that can be adapted for this. You could create a string to the effect "^a|^b|^c" and continue all the way to the letter 'z'. Let's call that string letter_match, which we'll use to match up with regex. Then, #find and extract matches #create a frequency table |
Are you able to use that to find words that start with and end with a, b, c....z? |
@ChadFibke
I just tried it with replacing letter_match with “a$|b$|c$” all the way to z. Checked the first few entries of `words` and it seems to work.
Of course, like you said though, you should use str_to_lower() to make `words` all lowercase (more so if you’re trying to find words beginning with each letter)
… On Nov 29, 2018, at 1:54 PM, FIBKE ***@***.***> wrote:
@bassamjaved <https://github.com/bassamjaved>,
Are you able to use that to find words that start with and end with a, b, c....z?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#535 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/Ao_FKra9gDTsKQJI_kosXSIQXWFbXgHmks5u0FetgaJpZM4Y6cV1>.
|
Ah but I see you want start and end with the same letter. Okay, no I haven't tried that with this particular method... |
@zeeva85 |
Thanks @ChadFibke |
Correct, sum the values then replace the 1:26 in 2nd column ("start_letter") This should work also i think
|
Yeah, the part of start and end with same letter is done.. I will try str_extract function here, thank you |
Nice, yeah I fogot to switch them to lower case, thanks for notice |
here's a revision of the method I posted earlier: #create a regular expression pattern that begin with a letter and ends with the same letter #collapse into one string #find and subset matches #extract letters in matches #create a frequency table |
Well @chenchenguo has multiple answers to choose from now! |
Hi, I met a problem when I want to implement a loop in regular expression.
What if I want to search a specified list of letters, like search "a", "b", "c", "d",..."z", sequentially?
right now, what I am implementing this through writing 26 regular expressions, which I know is stupid, but how to figure it out in just one loop or something else?
Thanks in advance.
The text was updated successfully, but these errors were encountered: