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

前端 未结 6 1224
陌清茗
陌清茗 2020-12-29 01:49

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

相关标签:
6条回答
  • 2020-12-29 02:12

    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']
    
    0 讨论(0)
  • 2020-12-29 02:18

    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

    0 讨论(0)
  • 2020-12-29 02:24

    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 "

    0 讨论(0)
  • 2020-12-29 02:24

    For .NET usage:

    \p{L}{3,}

    0 讨论(0)
  • 2020-12-29 02:33

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

    0 讨论(0)
  • 2020-12-29 02:38

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

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

    0 讨论(0)
提交回复
热议问题