我试图创建一个让我大开眼界的正则表达式。 当我有两个连续的数字时,如何在下面进行正则表达式否定?
/^([\p{L}\p{N}\. ]+)(, ?| )([0-9-]+[a-z]?)(, ?| |$)(.*)/iu
有效示例:
Text Text 123 anything
Text Text, 123, anything
Text Text 123B anything
Text Text, 123B, anything
Text 123 anything
Text 123B anything
Text, 123, anything
Text, 123B, anything
987 123 anything
987 123B anything
987, 123, anything
987, 123B, anything
(必须)无效示例:
Text Text 456 123 anything
Text Text, 456, 123, anything
Text Text 456 123B anything
Text Text, 456, 123B, anything
Text 456 123 anything
Text 456 123B anything
Text, 456, 123, anything
Text, 456, 123B, anything
987 456 123 anything
987 456 123B anything
987, 456, 123, anything
987, 456, 123B, anything
但是你们可以看到,上面的所有例子都适用于我的正则表达式:https://regex101.com/r/6t5Oq5/4
要求:第一组可能包含字母或数字。第二组可以有数字或数字后跟一个字母,第三组可以有任何东西。所有组都可以用逗号或空格分隔。所有字母和数字都可以是任意大小。 除非数字在第一组或最后一组(任何内容)中,否则字符串中不能有连续的数字。
这样做的最佳方式是什么?
答案 0 :(得分:2)
不是100%肯定你所需的规则,但这里的正则表达式匹配第一个但不是第二个块:
/^([a-z0-9]+,? )([0-9]+[a-z]?,? )([a-z0-9]+)$/
在这里演示:http://regexr.com/3gjd7
答案 1 :(得分:2)
根据您发布的内容,使用此模式^(\S+)(?=[^\d\r\n]+\d+[^\d\r\n]+$).*
Demo
^ # Start of string/line
( # Capturing Group (1)
\S # <not a whitespace character>
+ # (one or more)(greedy)
) # End of Capturing Group (1)
(?= # Look-Ahead
[^\d\r\n] # Character not in [\d\r\n] Character Class
+ # (one or more)(greedy)
\d # <digit 0-9>
+ # (one or more)(greedy)
[^\d\r\n] # Character not in [\d\r\n] Character Class
+ # (one or more)(greedy)
$ # End of string/line
) # End of Look-Ahead
. # Any character except line break
* # (zero or more)(greedy)