The numbers are all between 1 and 3 digits long and separated by spaces. I'm terrible with RegEx and don't even know where to start.
这些数字都在1到3位之间,用空格隔开。我很讨厌RegEx,甚至不知道从哪里开始。
Example:
例子:
59, 5, 53, 53, 545, 55, 545, 56, 5
I don't need to actually know which items are duplicates - just if there are any at all.
我不需要知道哪些项目是重复的——如果有的话。
3 Great answers, thanks a lot.
3个很好的答案,非常感谢。
4 个解决方案
#1
1
You can use "back-references" to solve this...
你可以使用“反向引用”来解决这个问题。
/(^|[^0-9])([0-9]+)(?=[^0-9]).*[^0-9]\2([^0-9]|$)/
The meaning is
意思是
-
(^|[0-9])
Start of the string OR a non-numeric - (^ |[0 - 9])字符串或一个非数字的开始
-
([0-9]+)
one or more digits - ([0-9]+)一个或多个数字
-
(?=[^0-9])
a non-numeric char, but don't take it - (? =[^ 0 - 9])一个非数字字符,但不要把它
-
.*
anything - *任何东西。
-
[^0-9]
a non-numeric char - (^ 0 - 9)一个非数字字符
-
\2
the same thing we found at 2 (it's the second parenthesized expression) - \2我们在2找到的东西(它是第二个括号)
-
($|[^0-9])
End of string OR a non-numeric char - ($ |[^ 0 - 9])的字符串或一个非数字字符
the reason for which a zero-width lookahead assertion (?=...)
is needed in (3) is because the two copies could be one right after the other in the sequence (with only one non-numeric character between them).
在(3)中需要一个零宽度的lookahead断言(?=…)的原因是,这两个副本可以在序列中的另一个后面(只有一个非数字字符)。
All the non-numeric/start-of-string/end-of-string trickery is needed because you don't want "1,2,3,4,33,9"
to match as duplicate because the beginning of 33
matches the 3
(you only want to consider full numbers, i.e. take all the consecutive digits to do the check).
所有非数字的/开始的/结束的字符串/结束的欺骗都是必需的,因为您不希望“1、2、3、4、33、9”匹配为重复的,因为33的开始与3匹配(您只想考虑全数,即取所有连续的数字进行检查)。
#2
2
Re: I don't need to actually know which items are duplicates - just if there are any at all.
Re:我不需要知道哪些项目是重复的,只要有的话。
It will match in case when string has duplicate numbers,
如果字符串有重复的数字,
/(\b\d+\b)(?=.*?\b\1\b)/
#3
2
\b(\d+)\b(?=.*\b\1\b)
Try this.See demo.
试试这个。看到演示。
https://regex101.com/r/uE3cC4/7
https://regex101.com/r/uE3cC4/7
#4
-2
Regular expressions are the wrong tool for this.
正则表达式是错误的工具。
Something like this Perl one-liner perhaps?
类似于Perl的一行程序?
perl -MList::MoreUtils=uniq -E"say join ', ', uniq shift =~ /\d+/g" "59, 5, 53, 53, 545, 55, 545, 56, 5"
output
输出
59, 5, 53, 545, 55, 56
#1
1
You can use "back-references" to solve this...
你可以使用“反向引用”来解决这个问题。
/(^|[^0-9])([0-9]+)(?=[^0-9]).*[^0-9]\2([^0-9]|$)/
The meaning is
意思是
-
(^|[0-9])
Start of the string OR a non-numeric - (^ |[0 - 9])字符串或一个非数字的开始
-
([0-9]+)
one or more digits - ([0-9]+)一个或多个数字
-
(?=[^0-9])
a non-numeric char, but don't take it - (? =[^ 0 - 9])一个非数字字符,但不要把它
-
.*
anything - *任何东西。
-
[^0-9]
a non-numeric char - (^ 0 - 9)一个非数字字符
-
\2
the same thing we found at 2 (it's the second parenthesized expression) - \2我们在2找到的东西(它是第二个括号)
-
($|[^0-9])
End of string OR a non-numeric char - ($ |[^ 0 - 9])的字符串或一个非数字字符
the reason for which a zero-width lookahead assertion (?=...)
is needed in (3) is because the two copies could be one right after the other in the sequence (with only one non-numeric character between them).
在(3)中需要一个零宽度的lookahead断言(?=…)的原因是,这两个副本可以在序列中的另一个后面(只有一个非数字字符)。
All the non-numeric/start-of-string/end-of-string trickery is needed because you don't want "1,2,3,4,33,9"
to match as duplicate because the beginning of 33
matches the 3
(you only want to consider full numbers, i.e. take all the consecutive digits to do the check).
所有非数字的/开始的/结束的字符串/结束的欺骗都是必需的,因为您不希望“1、2、3、4、33、9”匹配为重复的,因为33的开始与3匹配(您只想考虑全数,即取所有连续的数字进行检查)。
#2
2
Re: I don't need to actually know which items are duplicates - just if there are any at all.
Re:我不需要知道哪些项目是重复的,只要有的话。
It will match in case when string has duplicate numbers,
如果字符串有重复的数字,
/(\b\d+\b)(?=.*?\b\1\b)/
#3
2
\b(\d+)\b(?=.*\b\1\b)
Try this.See demo.
试试这个。看到演示。
https://regex101.com/r/uE3cC4/7
https://regex101.com/r/uE3cC4/7
#4
-2
Regular expressions are the wrong tool for this.
正则表达式是错误的工具。
Something like this Perl one-liner perhaps?
类似于Perl的一行程序?
perl -MList::MoreUtils=uniq -E"say join ', ', uniq shift =~ /\d+/g" "59, 5, 53, 53, 545, 55, 545, 56, 5"
output
输出
59, 5, 53, 545, 55, 56