我试图用正则表达式匹配字符串中的模式。想法是这样的。在
我有一个大小为n*m的数组,每个条目中都有G和非G。我必须找到G的模式,以加号(+)的形式,所有的加号的四个臂都有相同的大小。例如,在下面给出的示例中:
BGBBGB
GGGGGG
BGBBGB
GGGGGG
BGBBGB
BGBBGB
BGB
GGG
BGB
是加号(+)的一种形式,每个臂的大小为1。在
我试图用正则表达式来解决这个模式,但是,它对我来说并不奏效。在
^{pr2}$
只匹配水平轴上手臂长度相同的图案,中间有一个G。不知道如何匹配四个边上手臂长度相同的G的图案。感谢您的回答。在
Tags:
我会选择使用类似矩阵的方法来解决这个问题,而不是使用正则表达式。 在您的示例中,我假设
l
是一个类似于包含每一行数据的列表。在据我所知,正则表达式在一维数组中使用时是有用的,而不是像问题中那样用于二维数组。在
所以我倾向于说,使用正则表达式可能不是一个好方法。。。我试图将一个固定的图案与
G
在+
图案中的位置相匹配,每个手臂都有1个单位长:我的想法是在矩阵上滑动模式,然后在矩阵的每个子段上比较模式的每个片段。。。在
现在这是某种模式的“硬编码”。
但你可以从这一点出发,尝试让模式发展(不同的手臂长度)
注意:问题在于,使用正则表达式,您不能执行
\w{A}.\w{A}
这样的操作,A
将是一个共享计数器。。。至少我从没听说过。在相关问题 更多 >
编程相关推荐