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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With