正则表达式将最后一个空格之后的所有内容与最多2个或5个字符的单词匹配

首先,它需要匹配字符串的最后一个空格

"this is a test NL" with the code (.*\s) i get the word NL

"this is a test NL-NL" with the code (.*\s) i get the word NL-NL

"this is a test NL Search" with the code (.*\s) i get the word Search this is wrong. It should return nothing

The last word my have 2 or 5(with special char - or /) character. This is de code but it's not working

(.*\s)(\w{2}|\w-/{5})
评论
  • 雪
    回复

    You might use a capturing group with a character class and an alternation using | to match either 2 word chars or match 5 times one of the listed.

    ^.*\s(\w{2}|[\w/-]{5})$
    

    Regex demo

    Note that \s could also match a newline.