gpt4 book ai didi

php垂直正则表达式搜索

转载 作者:可可西里 更新时间:2023-11-01 13:13:27 25 4
gpt4 key购买 nike

我有一个描述 n x m 元素矩阵的字符串,如下所示:

§inputmap = "
~~~~~~~~~~~~~~~~~~~~B~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~BBB........BBB~~~~~~~~~~~~~
~~~~~~~~~~BB...............FBB~~~~~~~~~~
~~~~~~~~BB....................BB~~~~~~~~
~~~~~~BB.....F..................BB~~~~~~
~~~~~BB.....................F.....B~~~~~
~~~~B..............................B~~~~
~~~B........F.......................B~~~
~~BB.........F......................BB~~
~~B................F.................BB~
~BF....F....F........................FB~
~B.....................................B
B.....................................FB
B........F......F......................B
B...........................F..........B
B......................................B
B......................................B
B.......F.......................F......B
B......FFF.............................B
B.......F.............................FB
~B..................F.................FB
~BF...........................F.......B~
~~B...F...........F..........FFFFF.F.BB~
~~BB..................F..F....F.....BB~~
~~~B.......................FF.FF....B~~~
~~~~B..............................B~~~~
~~~~~BB...........................B~~~~~
~~~~~~BB........................BB~~~~~~
~~~~~~~~BB..........F..........B~~~~~~~~
~~~~~~~~~~BB................BB~~~~~~~~~~
~~~~~~~~~~~~~BBB.......F.BBB~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~BBBBBB~~~~~~~~~~~~~~~~~
";
$inputmap = trim($inputmap);

我需要构建一个正则表达式(或其他东西)来搜索字符串:

$search = "
*F*
FFF
*F*
";
$search = trim($search);

在整个网格上。另一方面,我需要找到一个由 5 个不同的字母“F”(3 个垂直和 3 个水平)组成的图案,以获取在 map 上找到的图案的行/列位置。

考虑到输入矩阵可能不同(5x5 或 10x10 或 20x25 或...),有没有办法用 php 和正则表达式解决我的问题?

最佳答案

您可以使用 $length=strstr($inputmap,"\n") 来计算每行的宽度。然后你可以构造一个正则表达式,它将找到一个 F,然后是 ($length-2) 其他字符,然后是 3 个 F,然后是 ($length-2) 其他字符,后跟一个 F。

关于php垂直正则表达式搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17026462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com