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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:04:12+00:00 2026-06-18T14:04:12+00:00

How can I allow single underscores only with preg_match? The regex I made below

  • 0

How can I allow single underscores only with preg_match?

The regex I made below takes double underscores which is not I want…

if(!preg_match('/^[a-z0-9\s\_]+$/', $name))
    {
        $error = true;
        echo '<error elementid="name" message="name - please use alpha-numbers and lowercaps only." />';
    }

For instance,

hello world                  ok
hello_world                  ok
hello__world                 not ok
hello_world__again__         not ok
_hello_world_again_          not ok
  • 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-18T14:04:13+00:00Added an answer on June 18, 2026 at 2:04 pm

    Including the _ inside []+ allows for multiples. You need to surround it in []+ but not include it inside. The entire structure is surrounded by ()+ to allow for multiples of it, but it always ends with letters, numbers, or spaces.

    /^([a-z0-9\s]+_?[a-z0-9]+[a-z0-9\s]+)+$/i
    

    Breaking it down:

    • Outer ()+ allows for multiples of the whole structure
    • [a-z0-9\s]+ Multiple letters, numbers, spaces (not underscore
    • _?[a-z0-9]+ optional single underscore inside the multiple letters/numbers/spaces, but not followed by space
    • [a-z0-9\s]+ more letters, numbers, spaces at the end (can’t end in underscore)
    • /i Case insensitive (remove the i if you need case sensitivity)

    // Updated to prevent space after underscore
    $patt = '/^([a-z0-9\s]+_?[a-z0-9]+[a-z0-9\s]+)+$/i';
    
    echo preg_match($patt, 'hello world');
    // 1
    echo preg_match($patt, 'hello_world');
    // 1
    echo preg_match($patt, 'hello_world_');
    // 0
    echo preg_match($patt, 'hello__world');
    // 0
    echo preg_match($patt, 'hello_world how_are you');
    // 1
    echo preg_match($patt, 'hello_world how_are you_');
    // 0
    echo preg_match($patt, 'hello_world__ how_are you_');
    // 0 
    echo preg_match($patt, 'hello_world how_ are you');
    // 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I allow single hyphens and single spaces only within words but not
I am trying to allow a single location (a file which does not exist
How can I annotate my model so I can allow only alphabets like A-Z
How can I allow spaces within an a numbers only validation, with a minimum
I'm looking for a WP plugin which can allow me to create different forms
Hi i want to validate a string with in which only numbers are allowed
I have a datagrid to which I bind some items. I only allow selecting
Currently, the example provided in QuickFIX/J, only allow single executor to talk with single
I want to define my own datatype that can hold a single one of
I have been using the following recipe to only allow a single instance of

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.