我正在尝试一个随机的正则表达式:
$string = "sajdk3:jdk3:jdk3:dgklmhij";
@arr = split(/([\da-z]+)([:;])\1\2\1/, $string);
# As per my understanding of RegEx, the given pattern shall match jdk3:jdk3:jdk3
# So @arr must contain two scalar values: 'sa' and ':dgklmhij'
# But when I printed @arr I got something else
print "Array: @arr\nNumber of items: ", scalar @arr;
#Array: sa jdk3 : :dgklmhij
#Number of items: 4
# So, I tried:
$string =~ /([\da-z]+)([:;])\1\2\1/;
print "\n( $1 ) ( $2 )\n";
print "($`)($&)($') \n";
# ( jdk3 ) ( : ) ( ) ( ) ( )
# (sa)(jdk3:jdk3:jdk3)(:dgklmhij)
有人可以解释为什么数组有 4 个元素,而不是 2 个吗?
好的,所以在@mapeec 的解释之后,我很好奇如何完成它。split
当在参数中不可避免地有一个捕获组时应该做什么。就像你想分割一个可以是12-05-92
or26.11.87
或的日期07 04 92
。