尝试这样的事情:
/(?<=[A-Z]+(?: [A-Z]+)*\b)(?:(?!\b\d{8}).)*/
基本上:
- 在后面查找所有大写字母或空格,然后是一个单词中断。
- 然后开始匹配,然后从那一点开始匹配,直到遇到断字后跟 8 位数字。
如果您的正则表达式引擎抱怨(如我的)可变长度看起来落后,请尝试以下操作:
/(?:[A-Z]+(?: [A-Z]+)*\b)((?:(?!\b\d{8}).)*)/
产量:
>> "TEST Hello, world, 75793250".match /(?:[A-Z]+(?: [A-Z]+)*\b)((?:(?!\b\d{8}).)*)/
=> #<MatchData "TEST Hello, world, " 1:" Hello, world, ">
>> "TEST TESTER Hello, world. Another word here. 75793250".match /(?:[A-Z]+(?: [A-Z]+)*\b)((?:(?!\b\d{8}).)*)/
=> #<MatchData "TEST TESTER Hello, world. Another word here. " 1:" Hello, world. Another word here. ">