Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a minimum/maximum character count for any character using a regular expression

Tags:

regex

I am trying to write a regular expression that will be used on a text box to validate its contents to see if it is between 1 and 35. The characters within the text box can be anything: numeric, alpha, punctuation, white space, etc. Here is what I have so far:

^[:;,\-@0-9a-zA-Zâéè'.\s]{1,35}$ 

As you can see, I have to list out all characters. Is there an easier way to say "all" characters?

like image 437
ben miles Avatar asked May 09 '12 15:05

ben miles


People also ask

How do I limit characters in regex?

The ‹ ^ › and ‹ $ › anchors ensure that the regex matches the entire subject string; otherwise, it could match 10 characters within longer text. The ‹ [A-Z] › character class matches any single uppercase character from A to Z, and the interval quantifier ‹ {1,10} › repeats the character class from 1 to 10 times.

How do you mention length in a regular expression?

Simple, complete and tested java code, for finding words of certain length n: int n = 10; String regex = "\\b\\w{" + n + "}\\b"; String str = "Hello, this is a test 1234567890"; ArrayList<String> words = new ArrayList<>(); final Pattern pattern = Pattern. compile(regex, Pattern.

What is the regular expression for characters?

A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.


1 Answers

Like this: .

The . means any character except newline (which sometimes is but often isn't included, check your regex flavour).

You can rewrite your expression as ^.{1,35}$, which should match any line of length 1-35.

like image 160
mkjeldsen Avatar answered Sep 21 '22 00:09

mkjeldsen