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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:43:15+00:00 2026-05-16T01:43:15+00:00

I need a regular expression that will allow only a to z and 0

  • 0

I need a regular expression that will allow only a to z and 0 to 9. I came across the function below on this site, but it allows a few symbols thru (#.-). How should it be done if it has to allow only a to z (both upper and lower case) and 0 to 9? I’m scared to edit it since I know nothing about regular expressions.

Also is this regular expression good to check for a to z and 0 to 9, or is there any way it can be bettered.

function isValid($str) {
    return !preg_match('/[^A-Za-z0-9.#\\-$]/', $str);
}

Thanks

  • 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-16T01:43:15+00:00Added an answer on May 16, 2026 at 1:43 am

    The following seems to be what you need in this case:

    function isValid($str) {
        return !preg_match('/[^A-Za-z0-9]/', $str);
    }
    

    The […] regex construct is called a character class. Something like [aeiou] matches one of any of the vowels.

    The [^…] is a negated character class, so [^aeiou] matches one of anything but the vowels (which includes consonants, digits, symbols, etc).

    The -, depending on where/how it appears in a character class definition, is a range definition, so 0-9 is the same as 0123456789.

    Thus, the regex [^A-Za-z0-9] actually matches a character that’s neither a letter nor a digit. This is why the result of preg_match is negated with !.

    That is, the logic of the above method uses double negation:

    isValid = it's not the case that
                  there's something other than a letter or a digit
                      anywhere in the string
    

    You can alternatively get rid of the double negation and use something like this:

    function isValid($str) {
        return preg_match('/^[A-Za-z0-9]*$/', $str);
    }
    

    Now there’s no negation. The ^ and $ are the beginning and of the string anchors, and * is a zero-or-one-of repetition metacharacter. Now the logic is simply:

    isValid = the entire string from beginning to end
                  is a sequence of letters and digits
    

    References

    • regular-expressions.info/Character Class, Anchors, Repetition

    Related questions

    • Regex: why doesn’t [01-12] range work as expected?
      • Detailed discussion, with common mistakes, etc
    • Character class subtraction, converting from Java syntax to RegexBuddy
      • Some flavors have rich character class arithmetics like subtraction and intersection

    Non-regex alternative

    Some languages have standard functions/idiomatic ways to validate that a string consists of only alphanumeric characters (among other possible string "types").

    In PHP, for example, you can use ctype_alnum.

    bool ctype_alnum ( string $text )

    Checks if all of the characters in the provided string , text, are alphanumeric.

    API links

    • PHP Ctype Functions – list of entire family of ctype functions
      • ctype_alpha, digit, lower, upper, space, etc
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression that will not allow multiple special characters in a
I need a regular expression that will match this pattern (case doesn't matter): 066B-E77B-CE41-4279
I need a regular expression that will get digits only if theres no letters
I need a regular expression that will accept well-formed emails in several formats (see
I need a regular expression that will match the first letter of a song
I need to write a regular expression that will be correct for telephone number
I need a regular expression that would let me validate Url. I found this
Hi I need a regular expression that'll give me something like this part ./something\,
I need an expression that will only accept: numbers normal letters (no special characters)
I need a regular expression that will replace odd number of slashes with even

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.