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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:27:23+00:00 2026-06-09T05:27:23+00:00

I want to match a String if it contains a maximum of 16 digits

  • 0

I want to match a String if it contains a maximum of 16 digits and a maximum of 11 characters, but with a maximum of 27 characters in total.

I tried (\d{0,16}\w{0,11}){27}, but it doesn’t seem to work.

Edit :

MATCH: 145214mkjnkj or 1234567891011125abcdefghijk (can be mixed)

DONT MATCH: abababababab123 or 12345678910111213abc

  • 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-09T05:27:25+00:00Added an answer on June 9, 2026 at 5:27 am

    Just get rid of the trailing quantifier, and use anchors to match the beginning and end of the string:

    /^(\d{0,16}\w{0,11})$/
    

    Where ^ matches start of string and $ matches end of string. Now this will match a string with 0 – 16 digits, followed by 0 – 11 word characters. Keep in mind \w includes [A-Za-z0-9_], if you want to match just alphabetic characters you can do this:

    /^(\d{0,16}[a-z]{0,11})$/i
    

    Edit: Since (apparently) digits and characters can be mixed, you can use one regex to test for invalid characters, then do some additional processing to determine if the minimum requirements for each character class are met with another regex, like so:

    1. The first is a check to see if the string contains any invalid characters. If this matches, we know the string is invalid:

      if( preg_match( '/[^\d\w]/', $input)) {
          echo "Invalid input - Only digits and characters allowed.\n";
      }
      

      Note: The above assumes \w for characters, for only alphabetic characters, use A-Za-z.

    2. If the above check passes, now we know the string contains only digits and characters, we test to see if it has enough characters by replacing all of the digits with an empty string, and checking if the remaining characters (which are only those in the \w family) are the correct length. Note the use of the by-reference $count parameter, named $replacements here, which will implicitly tell us how many digits there are in the string while we are checking for how many characters are in the string:

      $only_characters = preg_replace( '/\d/', '', $input, -1, $replacements);
      if( strlen( $only_characters) > 11) {
          echo "Invalid input - You can only have at most 11 characters\n";
      }
      if( $replacements > 16) {
          echo "Invalid input - You can only have at most 16 digits\n";
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you have a string with special characters that you want to match with:
To match string, which contains some word, I can use pattern /.*word.*/. But how
I want to match a string to make sure it contains only letters. I've
I want to match the following: any string which contains any character, except /
I want to write a regex that matches if a string contains XYZ but
If I want to match() a string with the following pattern: 12345678_12345678 What would
Let's say using Javascript, I want to match a string that ends with [abcde]*
I want to match and modify part of a string if following conditions are
Suppose I want to match abc within the string s only if it occurs
In this string: <0> <<1>> <2>> <3> <4> I want to match all instances

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.