计算字符串中的单词数

时间:2022-09-13 11:37:31

How can I match the number of words in a string to be > then 5 using regex?

如何使用正则表达式匹配字符串中的单词数> 5然后?

Input1: stack over flow => the regex will not match anything

Input1:堆栈溢出=>正则表达式不匹配任何东西

Input2: stack over flow stack over => the regex will match this string

Input2:堆栈超过流堆栈=>正则表达式将匹配此字符串

I have tried counting the spaces with /\/s/ but that didn't really helped me, because I need to match only strings with no of words > 5

我已经尝试使用/ \ / s /计算空格,但这并没有真正帮助我,因为我只需要匹配没有单词> 5的字符串

Also I don't want to use split by spaces.

另外我不想使用空格分割。

2 个解决方案

#1


4  

I would rely on a whitespace/non-whitespace patterns and allow trailing/leading whitespace:

我将依赖于空白/非空白模式并允许尾随/前导空格:

^\s*\S+(?:\s+\S+){4,}\s*$

See demo

Explanation:

  • ^ - start of string
  • ^ - 字符串的开头

  • \s* - optional any number of whitespace symbols
  • \ s * - 可选任意数量的空白符号

  • \S+ - one or more non-whitespace symbols
  • \ S + - 一个或多个非空白符号

  • (?:\s+\S+){4,} - 4 or more sequences of one or more whitespace symbols followed with one or more non-whitespace symbols
  • (?:\ s + \ S +){4,} - 一个或多个空白符号的4个或更多序列,后跟一个或多个非空白符号

  • \s* - zero or more (optional) trailing whitespace symbols
  • \ s * - 零个或多个(可选)尾随空格符号

  • $ - end of string
  • $ - 结束字符串

#2


5  

^ *\w+(?: +\w+){4,}$

You can use this regex.See demo.

你可以使用这个regex.See演示。

https://regex101.com/r/cZ0sD2/15

#1


4  

I would rely on a whitespace/non-whitespace patterns and allow trailing/leading whitespace:

我将依赖于空白/非空白模式并允许尾随/前导空格:

^\s*\S+(?:\s+\S+){4,}\s*$

See demo

Explanation:

  • ^ - start of string
  • ^ - 字符串的开头

  • \s* - optional any number of whitespace symbols
  • \ s * - 可选任意数量的空白符号

  • \S+ - one or more non-whitespace symbols
  • \ S + - 一个或多个非空白符号

  • (?:\s+\S+){4,} - 4 or more sequences of one or more whitespace symbols followed with one or more non-whitespace symbols
  • (?:\ s + \ S +){4,} - 一个或多个空白符号的4个或更多序列,后跟一个或多个非空白符号

  • \s* - zero or more (optional) trailing whitespace symbols
  • \ s * - 零个或多个(可选)尾随空格符号

  • $ - end of string
  • $ - 结束字符串

#2


5  

^ *\w+(?: +\w+){4,}$

You can use this regex.See demo.

你可以使用这个regex.See演示。

https://regex101.com/r/cZ0sD2/15