- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
注意:这是一个关于现代正则表达式的可能性的问题。这不是使用其他方法解决此问题的最佳方法。它的灵感来自 an earlier question ,但不限于正则表达式。
在 ASCII“图像”/art/map/string 中,例如:
....X.......
..X..X...X....
X.X...X..X.....
X....XXXXXX.....
X..XXX...........
.....X..........
..............X
..X...........X....
..X...........X....X...
....X.....
我想找一个由三个X
组成的简单垂直线:
X
X
X
图像中的行数是可变的,每一行的宽度也是可变的。
使用正则表达式(PCRE/PHP、Perl、.NET 或类似)是否可以:
最佳答案
要回答可以使用的第一个问题:
(?xm) # ignore comments and whitespace, ^ matches beginning of line
^ # beginning of line
(?:
. # any character except \n
(?= # lookahead
.*+\n # go to next line
( \1?+ . ) # add a character to the 1st capturing group
.*+\n # next line
( \2?+ . ) # add a character to the 2nd capturing group
)
)*? # repeat as few times as needed
X .*+\n # X on the first line and advance to next line
\1?+ # if 1st capturing group is defined, use it, consuming exactly the same number of characters as on the first line
X .*+\n # X on the 2nd line and advance to next line
\2?+ # if 2st capturing group is defined, use it, consuming exactly the same number of characters as on the first line
X # X on the 3rd line
此表达式适用于 Perl、PCRE、Java 并且应该适用于 .NET。
该表达式使用带有自引用捕获组的前瞻来为前瞻的每次重复添加一个字符(这用于“计数”)。
\1?+
表示如果 \1
匹配(或已定义)使用它,并且不将其归还(不要回溯)。在这种情况下,它等效于 (?(1)\1)
。这意味着如果 \1
已定义,则匹配 \1
。
polygenelubricants在 his answer for How can we match a^n b^n with Java regex? 中很好地解释了这种带有反向引用的前瞻。 . (他还写过关于 Java 正则表达式的其他令人印象深刻的技巧,涉及反向引用和环视。)
当仅使用匹配并要求匹配数中的答案(计数)时,问题 2 的答案将是:
它可以不直接在具有有限后视的正则表达式风格中解决。而像 Java 和 .NET 这样的其他风格可以(例如在 m.buettner's .NET solution 中)。
因此,在这种情况下,Perl 和 PCRE(PHP 等)中的普通正则表达式匹配无法直接回答这个问题。
假设没有可用的可变长度后视。
您必须以某种方式计算 X
之前一行中的字符数。
做到这一点的唯一方法是匹配它们,并且由于没有可用的可变长度后视,您必须(至少)在行首开始匹配。
如果从一行的开头开始匹配,则每行最多只能匹配一个。
由于每行可能出现多次,因此不会全部计算在内,也不会给出正确答案。
另一方面,如果我们接受答案作为匹配或替换结果的长度,那么第二个问题可以在 PCRE 和 Perl(以及其他风格)中回答。
此解决方案基于m.buettner's nice "partial PCRE solution" 或受其启发.
可以简单地将以下表达式的所有匹配项替换为 $3
,得到问题二的答案(兴趣模式的数量)作为结果字符串的长度。
^
(?:
(?: # match .+? characters
.
(?= # counting the same number on the following two lines
.*+\n
( \1?+ . )
.*+\n
( \2?+ . )
)
)+?
(?<= X ) # till the above consumes an X
(?= # that matches the following conditions
.*+\n
\1?+
(?<= X )
.*+\n
\2?+
(?<= X )
)
(?= # count the number of matches
.*+\n
( \3?+ . ) # the number of matches = length of $3
)
)* # repeat as long as there are matches on this line
.*\n? # remove the rest of the line
在 Perl 中可以写成:
$in =~ s/regex/$3/gmx;
$count = length $in;
此表达式类似于上面问题 1 的解决方案,但进行了一些修改,以在第一次前瞻匹配的字符中包含 X
,并用量词包裹并计算量词的匹配次数。
除了直接匹配之外,它尽可能接近(除了正则表达式之外的额外代码),并且可能是问题 2 的可接受答案。
上述解决方案的一些测试用例和结果。结果显示数字答案(结果字符串的长度),括号中是替换后的结果字符串。
Test #0:
--------------------
X
X
X
result: 1 (X)
Test #1:
--------------------
..X....
..X....
..X....
result: 1 (.)
Test #2:
--------------------
..X.X..
..X.X..
....X..
result: 1 (.)
Test #3:
--------------------
..X....
..X....
...X...
result: 0 ()
Test #4:
--------------------
..X....
...X...
..X....
result: 0 ()
Test #5:
--------------------
....X..
.X..X..
.X.....
result: 0 ()
Test #6:
--------------------
.X..X..
.X.X...
.X.X...
result: 1 (.)
Test #7:
--------------------
.X..X..
.X..X..
.X..X..
result: 2 (.X)
Test #8:
--------------------
XXX
XXX
XXX
result: 3 (XXX)
Test #9:
--------------------
X.X.X
XXXXX
XXXXX
.X.X.
result: 5 (XXXXX)
Test #10:
--------------------
1....X.......
2..X..X...X....
3X.X...X..X.....
4X....XXXXXX.....
5X..XXX...........
6.....X..........
7.........X....X
8..X......X....X....
9..X......X....X....X...
A....X.....
B.X..X..
C.....
XXX
XXX
XXX
.
result: 8 (3458.XXX)
关于php - "vertical"正则表达式匹配 ASCII "image",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039670/
我有一个加号/减号按钮,希望用户不能选择超过 20 个但不知道如何让它工作。我尝试使用 min="1"max="5 属性,但它们不起作用。这是我的代码和一个 fiddle 链接。https://jsf
我正在尝试复制顶部底部图,如示例 here但它没有正确渲染(紫色系列有 +ve 和 -ve 值,绿色为负值)留下杂乱的人工制品。我也在努力创建一个玩具示例来复制这个问题,所以我希望尽管我缺乏数据,但有
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 6 年前。 社区去年审查了是
这个问题在这里已经有了答案: Adding two positive integers gives negative answer.Why? (4 个答案) 关闭 5 年前。 我遇到了一个奇怪的问题
有谁知道如何将字符串值类型 -4,5 或 5,4 转换为 double -4.5 或 5.4? 最佳答案 只需使用 Double.parseDouble(Locale, String); 糟糕,我很困
我正在尝试根据 TextBlob 分类插入一个仅包含“正”或“负”字符串的新数据框列:对于我的 df 的第一行,结果是 ( pos , 0.75, 0.2499999999999997)我想要' 正
我对 VBA 非常陌生,无法理解如何在一个循环中完成 2 个任务。我非常感谢您的帮助。 我已经能够根据第 3 列中的数据更改第 2 列中的数值,但我不明白如何将负值的字体更改为红色。 表格的大小每月都
欢迎, 我正在使用 jquery 通过 POST 发送表单。 这就是我获得值(value)的方式。 var mytext = $("#textareaid").val(); var dataStrin
double d = 0; // random decimal value with it's integral part within the range of Int32 and always p
我有这个字符串: var a='abc123#xyz123'; 我想构建 2 个正则表达式替换函数: 1) 用 '*' 替换所有确实有 future '#'的字符(不包括'#') 所以结果应该是这样的
我正在使用 DialogFragment。当用户从 Gmail 平板电脑应用程序的屏幕与下面示例图片中的编辑文本进行交互时,我希望正面和负面按钮保持在键盘上方。 在我的尝试中不起作用,这是我的 Dia
从组装艺术一书中,我复制了这句话: In the two’s complement system, the H.O. bit of a number is a sign bit. If the H.O
是否有更好更优雅的方法来实现下面的简单代码(diffYear、A 和 B 是数字): diffYear = yearA - yearB; if (diffYear == 0) { A = B
我正在设计一种语言,并尝试确定 true 应该是 0x01 还是 0xFF。显然,所有非零值都将转换为 true,但我正在尝试确定确切的内部表示。 每种选择的优点和缺点是什么? 最佳答案 没关系,只要
在我的 dialogfragment 类的 OnCreateDialog 中,我正在这样做: AlertDialog.Builder builder = new AlertDialog.Builder
这个问题在这里已经有了答案: Resolving ambiguous overload on function pointer and std::function for a lambda usin
我偶然发现了一个奇怪的 NSDecimalNumber 行为:对于某些值,调用 integerValue、longValue、longLongValue 等,返回意想不到的值(value)。示例: l
这个问题在这里已经有了答案: Resolving ambiguous overload on function pointer and std::function for a lambda using
我有这个正则表达式来测试用户输入是否有效: value.length === 0 || value === '-' || (!isNaN(parseFloat(value)) && /^-?\d+\.
我想用高斯混合模型拟合数据集,数据集包含大约 120k 个样本,每个样本有大约 130 个维度。当我使用 matlab 执行此操作时,我运行脚本(簇号为 1000): gm = fitgmdist(d
我是一名优秀的程序员,十分优秀!