Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7580611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:57:39+00:00 2026-05-30T17:57:39+00:00

In R, grep usually matches a vector of multiple strings against one regexp. Q:

  • 0

In R, grep usually matches a vector of multiple strings against one regexp.

Q: Is there a possibility to match a single string against multiple regexps? (without looping through each single regexp pattern)?

Some background:

I have 7000+ keywords as indicators for several categories. I cannot change that keyword dictionary. The dictionary has following structure (keywords in col 1, numbers indicate categories where these keywords belong to):

ab  10  37  41
abbrach*    38
abbreche    39
abbrich*    39
abend*  37
abendessen* 60  63
aber    20  23  45
abermals    37

Concatenating so many keywords with “|” is not a feasible way (and I wouldn’t know which of the keywords generated the hit).
Also, just reversing “patterns” and “strings” does not work, as the patterns have truncations, which wouldn’t work the other way round.

[related question, other programming language]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:57:41+00:00Added an answer on May 30, 2026 at 5:57 pm

    What about applying the regexpr function over a vector of keywords?

    keywords <- c("dog", "cat", "bird")
    
    strings <- c("Do you have a dog?", "My cat ate by bird.", "Let's get icecream!")
    
    sapply(keywords, regexpr, strings, ignore.case=TRUE)
    
         dog cat bird
    [1,]  15  -1   -1
    [2,]  -1   4   15
    [3,]  -1  -1   -1
    
        sapply(keywords, regexpr, strings[1], ignore.case=TRUE)
    
     dog  cat bird 
      15   -1   -1 
    

    Values returned are the position of the first character in the match, with -1 meaning no match.

    If the position of the match is irrelevant, use grepl instead:

    sapply(keywords, grepl, strings, ignore.case=TRUE)
    
           dog   cat  bird
    [1,]  TRUE FALSE FALSE
    [2,] FALSE  TRUE  TRUE
    [3,] FALSE FALSE FALSE
    

    Update: This runs relatively quick on my system, even with a large number of keywords:

    # Available on most *nix systems
    words <- scan("/usr/share/dict/words", what="")
    length(words)
    [1] 234936
    
    system.time(matches <- sapply(words, grepl, strings, ignore.case=TRUE))
    
       user  system elapsed 
      7.495   0.155   7.596 
    
    dim(matches)
    [1]      3 234936
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually use the following pipeline to grep for a particular search string and
I have a file consisting of digits. Usually, each line contains one single number.
I want to grep for the string that starts with a dash/hyphen, like -X
Using only grep and sed, is there a way I can tranform the output
Can grep pick lines if at least one element from a list of words
I want to grep a particular string from a text file at some location
Is there any way I could use grep to ignore some files when searching
How do i search/ grep for a string only from the list of files
How can I use grep to show just file-names (no in-line matches) on Linux?
grep is used to search within a file to see if any line matches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.