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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:10:58+00:00 2026-06-12T21:10:58+00:00

I used preg_match() to blacklist some special characters, my requirement is whenever the string

  • 0

I used preg_match() to blacklist some special characters, my requirement is whenever the string contains such special characters, I would return an invalid.

$str = "adasdasasd*";
if(preg_match('/^[^~`!@#$%\^&\*\(\)]+$/',$str)) {
  echo "valid";
}
else {
  echo "invalid";
}

This returns invalid, which is correct. However, up until now I’m quite confused with this negate on preg_match. Can someone have at least a brief explanation of negate on preg_match()? And also, is the regular expression I provided to preg_match() has any downside?

  • 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-12T21:11:00+00:00Added an answer on June 12, 2026 at 9:11 pm

    Ok, so your regular expression contains a range specifier, in this case, [^~!@#$%\^&\*\(\)]+. That’s a little complex, so let’s simplify for now to [abc].

    [abc] will match any letter which is a, b or c, as you probably know. Adding in a ^ first negates this set, so [^abc] matches any letter that is not a, b or c, so will match d, e, f, g, H, 4, £ and so on.

    In your case, you are matching anything that is not in your list of special characters. You’re saying “match start of string, then match one or more ‘non-special’ characters, then match the end of the string”. If you have any special characters, they cannot be matched by the “non-special” character match, or anything else, thus the regex fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some string in which I would like to check if contains some
I've used preg_match_all's PREG_PATTERN_ORDER to return positions in the values found in a string.
I have used preg_match to check whether a string matches a given regular expression,
i used this code: List<string> lists=new List<string>(apple,orange,banana,apple,mang0,orange); string names; names=lists.Distinct() is that correct?
Is there a canonical function/method for escaping a string to be used in a
I need to use preg_match to check that only a-z0-9. is used (including full
In PHP, what is a list of potentially harmful characters that can be used
I have used this... I saw this in some site.. But it didn't help
I have used this pregmatch statement for validating email address preg_match(^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$^, $partner_email) If i
I would like to create a preg_match function to validate my passowrds, but I'm

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.