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 9215811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:17:10+00:00 2026-06-18T02:17:10+00:00

I need to validate a string against a character vector pattern. My current code

  • 0

I need to validate a string against a character vector pattern. My current code is:

trim <- function (x) gsub("^\\s+|\\s+$", "", x)

# valid pattern is lowercase alphabet, '.', '!', and '?' AND
# the string length should be >= than 2
my.pattern = c(letters, '!', '.', '?')

check.pattern = function(word, min.size = 2)
{
    word = trim(word)
    chars = strsplit(word, NULL)[[1]]
    all(chars %in% my.pattern) && (length(chars) >= min.size)
}

Example:

w.valid = 'special!'
w.invalid = 'test-me'

check.pattern(w.valid) #TRUE
check.pattern(w.invalid) #FALSE

This is VERY SLOW i guess…is there a faster way to do this? Regex maybe?
Thanks!

PS: Thanks everyone for the great answers. My objective was to build a 29 x 29 matrix,
where the row names and column names are the allowed characters. Then i iterate over each word of a huge text file and build a ‘letter precedence’ matrix. For example, consider the word ‘special’, starting from the first char:

row s, col p -> increment 1
row p, col e -> increment 1
row e, col c -> increment 1
... and so on.

The bottleneck of my code was the vector allocation, i was ‘appending’ instead of pre-allocate the final vector, so the code was taking 30 minutes to execute, instead of 20 seconds!

  • 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-18T02:17:12+00:00Added an answer on June 18, 2026 at 2:17 am

    There are some built-in functions that can clean up your code. And I think you’re not leveraging the full power of regular expressions.

    The blaring issue here is strsplit. Comparing the equality of things character-by-character is inefficient when you have regular expressions. The pattern here uses the square bracket notation to filter for the characters you want. * is for any number of repeats (including zero), while the ^ and $ symbols represent the beginning and end of the line so that there is nothing else there. nchar(word) is the same as length(chars). Changing && to & makes the function vectorized so you can input a vector of strings and get a logical vector as output.

    check.pattern.2 = function(word, min.size = 2)
    {
        word = trim(word)
        grepl(paste0("^[a-z!.?]*$"),word) & nchar(word) >= min.size
    }
    check.pattern.2(c(" d ","!hello  ","nA!","  asdf.!"," d d "))
    #[1] FALSE  TRUE FALSE  TRUE FALSE
    

    Next, using curly braces for number of repetitions and some paste0, the pattern can use your min.size:

    check.pattern.3 = function(word, min.size = 2)
    {
        word = trim(word)
        grepl(paste0("^[a-z!.?]{",min.size,",}$"),word)
    }
    check.pattern.3(c(" d ","!hello  ","nA!","  asdf.!"," d d "))
    #[1] FALSE  TRUE FALSE  TRUE FALSE
    

    Finally, you can internalize the regex from trim:

    check.pattern.4 = function(word, min.size = 2)
    {
        grepl(paste0("^\\s*[a-z!.?]{",min.size,",}\\s*$"),word)
    }
    check.pattern.4(c(" d ","!hello  ","nA!","  asdf.!"," d d "))
    #[1] FALSE  TRUE FALSE  TRUE FALSE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex or a function in PHP that will validate a string
I need to validate file name with following pattern.... File name(string) should not be
I need the absoulute fastest way possible to validate an input string against a
I've a quick function to load up an XML string, and validate it against
I need to be able to validate a string against a list of the
I need to validate an XML string (and not a file) against a DTD
I need to validate input to my application. The input is a formatted string
I need to validate a date string in a specific format in Javascript. The
I need to validate a string that contains XML Data, there is no schema
I need to validate a XML file against a schema. The XML file is

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.