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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:26:22+00:00 2026-06-16T13:26:22+00:00

I am stuck trying to create a regex that will allow for letters, numbers,

  • 0

I am stuck trying to create a regex that will allow for letters, numbers, and the following chars: _ – ! ? . ,

Here is what I have so far:

/^[-\'a-zA-Z0-9_!\?,.\s]+$/      //not escaping the ?

and this version too:

/^[-\'a-zA-Z0-9_!\?,.\s]+$/     //attempting to escape the ? 

Neither of these seem to be able to match the following:

“Oh why, oh why is this regex not working! It’s getting pretty frustrating? Frustrating – that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?”

Can somebody point out what I am doing wrong? I must point out that my script takes the user input (the paragraph in quotes in this case) and strips all white space so actual input has no white space.

Thanks!

UPDATE:
Thanks to Lix’s advice, this is what I have so far:

/^[-\'a-zA-Z0-9_!\?,\.\s]+$/

However, it’s still not working??

UPDATE2
Ok, based on input this is what’s happening.
User inputs string, then I run the string through following functions:

$comment = preg_replace('/\s+/', '',   
htmlspecialchars(strip_tags(trim($user_comment_orig))));

So in the end, user input is just a long string of chars without any spaces. Then that string of chars is run using:

preg_match("@^[-_!?.,a-zA-Z0-9]+$@",$comment) 

What could possibly be causing trouble here?

FINAL UPDATE:

Ended up using this regex:

"@[-'A-Z0-9_?!,.]+@i"

Thanks all! lol, ya’ll are going to kill me once you find out where my mistake was!

Ok, so I had this piece of code:

if(!preg_match($pattern,$comment) || strlen($comment) < 2 || strlen($comment) > 60){

GEEZ!!! I never bothered to look at the strlen part of the code. Of course it was going to fail every time…I only allowed 60 chars!!!!

  • 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-16T13:26:24+00:00Added an answer on June 16, 2026 at 1:26 pm

    When in doubt, it’s always safe to escape non alphanumeric characters in a class for matching, so the following is fine:

    /^[\-\'a-zA-Z0-9\_\!\?\,\.\s]+$/

    When run through a regular expression tester, this finds a match with your target just fine, so I would suggest you may have a problem elsewhere if that doesn’t take care of everything.

    I assume you’re not including the quotes you used around the target when actually trying for a match? Since you didn’t build double quote matching in…

    Can somebody point out what I am doing wrong? I must point out that my script takes the user input (the paragraph in quotes in this case) and strips all white space so actual input has no white space.

    in which case you don’t need the \s if it’s working correctly.

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

Sidebar

Related Questions

Hi I am trying to create a regex that will do the following grab
I'm stuck with trying to create a function that will do the following: let
I'm trying to create a Regex javascript split, but I'm totally stuck. Here's my
I'm stuck trying to solve this. I'm plotting some information that I have in
I'm stuck trying to create a result page that lists details based on a
I'm trying create a query that will output a total number, as well as
Im trying to create a payment system that integrates with eWay .. They have
i am trying to create the following mysql procedure, but i am stuck for
So I have been stuck at this problem for days now, trying to create
im trying to create a restaurant system that will create food items and such

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.