David
David

Reputation: 4475

How do I use a regular expression to match any string, but at least 3 characters?

I am not a regex expert, but my request is simple: I need to match any string that has at least 3 or more characters that are matching.

So for instance, we have the string "hello world" and matching it with the following:

"he" => false // only 2 characters
"hel" => true // 3 characters match found

Upvotes: 43

Views: 132590

Answers (6)

dsa
dsa

Reputation: 41

I tried find similiar as topic first post.

For my needs I find this

http://answers.oreilly.com/topic/217-how-to-match-whole-words-with-a-regular-expression/

"\b[a-zA-Z0-9]{3}\b"

3 char words only "iokldöajf asd alkjwnkmd asd kja wwda da aij ednm <.jkakla "

Upvotes: 4

Jerry
Jerry

Reputation: 4408

If you want to match starting from the beginning of the word, use:

\b\w{3,}

\b: word boundary

\w: word character

{3,}: three or more times for the word character

Upvotes: 4

user297250
user297250

Reputation:

This is python regex, but it probably works in other languages that implement it, too.

I guess it depends on what you consider a character to be. If it's letters, numbers, and underscores:

\w{3,}

if just letters and digits:

[a-zA-Z0-9]{3,}

Python also has a regex method to return all matches from a string.

>>> import re
>>> re.findall(r'\w{3,}', 'This is a long string, yes it is.')
['This', 'long', 'string', 'yes']

Upvotes: 61

alkokaine
alkokaine

Reputation: 11

For .NET usage:

\p{L}{3,}

Upvotes: 0

dillisingh
dillisingh

Reputation: 149

You could try with simple 3 dots. refer to the code in perl below

$a =~ m /.../ #where $a is your string

Upvotes: 1

alejandrobog
alejandrobog

Reputation: 2101

Try this .{3,} this will match any characher except new line (\n)

Upvotes: 14

Related Questions