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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:41:40+00:00 2026-05-24T05:41:40+00:00

when I try preg_match with the following expression: /.{0,5}/ , it still matches string

  • 0

when I try preg_match with the following expression: /.{0,5}/, it still matches string longer than 5 characters.
It does, however, work properly when trying in online regexp matcher

  • 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-24T05:41:43+00:00Added an answer on May 24, 2026 at 5:41 am

    The site you reference, myregexp.com, is focussed on Java.

    Java has a specific function for matching an exact pattern, without needing to use anchor characters. This is the function which myregexp.com uses.

    In most other languages, in order to match an exact pattern, you would need to add the anchoring characters ^ and $ at the start and end of the pattern respectively, otherwise the regex assumes it only needs to find the matched pattern somewhere within the string, rather than the whole string being the match.

    This means that without the anchors, your pattern will match any string, of any length, because whatever the string, it will contain within it somewhere a match for “zero to five of any character”.

    So in PHP, and Perl, and virtually any other language, you need your pattern to look like this:

    /^.{0,5}$/
    

    Having explained all that, I would make one final observation though: this specific pattern really doesn’t need to be a regular expression — you could achieve the same thing with strlen(). In addition, the dot character in regex may not work exactly as you expect: it typically matches almost any character; some characters, including new line characters, are excluded by default, so if your string contains five characters, but one of them is a new line, it will fail your regex when you might have expected it to pass. With this in mind, strlen() would be a safer option (or mb_strlen() if you expect to have unicode characters).

    If you need to match any character in regex, and the default behaviour of the dot isn’t good enough, there are two options: One is to add the s modifier at the end of the expression (ie it becomes /^.{0,5}$/s). The s modifier tells regex to include new line characters in the dot “any character” match.

    The other option (which is useful for languages that don’t support the s modifier) is to use an expression and its negative together in a character class – eg [\s\S] – instead of the dot. \s matches any white space character, and \S is a negative of \s, so any character not matched by \s. So together in a character class they match any character. It’s more long winded and less readable than a dot, but in some languages it’s the only way to be sure.

    You can find out more about this here: http://www.regular-expressions.info/dot.html

    Hope that helps.

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

Sidebar

Related Questions

I am trying to use the following regular expression to check whether a string
I'm trying to create a string from a regular expression. I noticed that in
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
Try executing the following in JavaScript: parseInt('01'); //equals 1 parseInt('02'); //equals 2 parseInt('03'); //equals
// create a string $string = '+7'; // try to match the beginning of
I'm try to extract the INV part of the string below: 123_456_P1234_INV_UID-123456.PDF Here's the
I have for example a string \try Tester234 where I want to find the
I try to parse url with a regular expression in order to capture element,
I'm having an issue properly detecting my user agent against a string of agents,

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.