Solved

How to use Regular Expression to check for minimum string length

Posted on 2004-10-07
8
8,223 Views
Last Modified: 2012-05-05

Hi,

How do I use a Regular Expression to find out  if a string is at least N characters?

Thanks.
0
Comment
Question by:l_agmon
  • 3
  • 2
8 Comments
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12247276
/^.{5}$/

or

/^.....$/



0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12247284
Sorry didnt read 'atleast'.

/^.{5}.*
/^......*$/    (remember to put 6 dots, * stands for any number of including 0)
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12247311
Im sorry, was in a bad state while replying, so assumed it as 5.

/^.{N}.*/   replace N with the number of characters. AFAIK, you cannot use a variable here in a $ format.

second option is
/^......*/  - but do remember to put N+1 dots. This is because * works on the last dot and it means, any number of any character including zero characters. . so N dots means N characters and follow it by a .* for any number of characters including zero. So the whole thing reads like, N characters followed by any number of characters.....

Cheers,
Manav
0
 
LVL 6

Accepted Solution

by:
DominicCronin earned 125 total points
ID: 12260603
Of course, the correct answer to your question depends on which regex language you are using. It's not uncommon for a regex language to allow you to specify "both ends"  of an interval quantifier. Using several dots is one possibility, but let's assume Perl regexes for a moment. The interval quantifier allows you to say {n,m} meaning match n to m times. This means that

.{5,}

will match at least 5 characters. In perl this would look like:

/.{5,}/

Note that manav's use of anchors (^ and *) isn't necessary if all you want to do is check that the string is at least that long. Still - I suppose you want to make this part of some more complicated test. Using regexes just to check a string length is overkill. Most programming languages (all programming languages??) have simpler ways to get the length of a string.
0
 
LVL 6

Expert Comment

by:DominicCronin
ID: 12477505
I think the correct answer is "you shouldn't". Anyway - sufficient good suggestions have been given to award the points and PAQ the question.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question