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

  • SEARCH
  • Home
  • 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 8966883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:07:10+00:00 2026-06-15T17:07:10+00:00

I have string and character vector. I would like to find all strings in

  • 0

I have string and character vector. I would like to find all strings in character vector matching as much as possible characters from beging of string.
For example:

s <- "abs"
vc <- c("ab","bb","abc","acbd","dert")

result <- c("ab","abc")

String s should be matched exactly up to first K characters. I want match for as much as possible (max K<=length(s)).
Here there is no match for “abs” (grep(“abs”,vc)), but for “ab” there are two matches (result <-grep(“ab”,vc)).

  • 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-06-15T17:07:12+00:00Added an answer on June 15, 2026 at 5:07 pm

    Another interpretation:

    s <- "abs"
    # Updated vc
    vc <- c("ab","bb","abc","acbd","dert","abwabsabs")
    
    st <- strsplit(s, "")[[1]]
    mtc <- sapply(strsplit(substr(vc, 1, nchar(s)), ""), 
                  function(i) {
                    m <- i == st[1:length(i)]
                    sum(m * cumsum(m))})
    
    vc[mtc == max(mtc)]
    #[1] "ab"        "abc"       "abwabsabs"
    
    # Another vector vc
    vc <- c("ab","bb","abc","acbd","dert","absq","abab")
    ....
    vc[mtc == max(mtc)]
    #[1] "absq"
    

    Since we are considering only beginnings of strings, in the first case the longest match was "ab", even though there is "abwabsabs" which has "abs".

    Edit: Here is a “single pattern” solution, possibly it could be more concise, but here we go…

    vc <- c("ab","bb","abc","acbd","dert","abwabsabs")
    (auxOne <- sapply((nchar(s)-1):1, function(i) substr(s, 1, i)))
    #[1] "ab"   "a"
    (auxTwo <- sapply(nchar(s):2, function(i) substring(s, i)))
    #[1] "s" "bs" 
    l <- attr(regexpr(
      paste0("^((",s,")|",paste0("(",auxOne,"(?!",auxTwo,"))",collapse="|"),")"),
      vc, perl = TRUE), "match.length")
    vc[l == max(l)]
    #[1] "ab"        "abc"       "abwabsabs"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a std::vector<std::string> which would contain numbers and characters (single char). I would
I have character vector of strings like this : x <- c(weather is good_today,it.
I am working in R and I have a character vector. I would like
I have a vector of strings: x<-c(abc.dat, xyz.dat) First I would like to replace
I have a string. How do I remove all text after a certain character?
Possible Duplicate: Count specific character occurances in string I have a delimeter in string
I have a string that contains a czech character. the string is 0bálka This
I have a big string having at most 100000 character. Instead of using string.charAt[index]
I have a very long String and I want to read character by character
I have a query that returns a string, as well as an escape character

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.