Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match words of a certain length

Tags:

java

regex

I would like to know the regex to match words such that the words have a maximum length. for eg, if a word is of maximum 10 characters in length, I would like the regex to match, but if the length exceeds 10, then the regex should not match.

I tried

^(\w{10})$ 

but that brings me matches only if the minimum length of the word is 10 characters. If the word is more than 10 characters, it still matches, but matches only first 10 characters.

like image 692
Anand Hemmige Avatar asked Jan 28 '12 08:01

Anand Hemmige


People also ask

How do you restrict length 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 I find the length of a word in regex?

To check the length of a string, a simple approach is to test against a regular expression that starts at the very beginning with a ^ and includes every character until the end by finishing with a $.

How do I match a range in regex?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. Something like ^[2-9][1-6]$ matches 21 or even 96! Any help would be appreciated.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


2 Answers

I think you want \b\w{1,10}\b. The \b matches a word boundary.

Of course, you could also replace the \b and do ^\w{1,10}$. This will match a word of at most 10 characters as long as its the only contents of the string. I think this is what you were doing before.

Since it's Java, you'll actually have to escape the backslashes: "\\b\\w{1,10}\\b". You probably knew this already, but it's gotten me before.

like image 98
Tikhon Jelvis Avatar answered Oct 26 '22 11:10

Tikhon Jelvis


^\w{0,10}$ # allows words of up to 10 characters. ^\w{5,}$   # allows words of more than 4 characters. ^\w{5,10}$ # allows words of between 5 and 10 characters. 
like image 39
Tim Pietzcker Avatar answered Oct 26 '22 10:10

Tim Pietzcker