- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个语法,我正在尝试借助 Regexp::Grammars 进行解析,但出于某种原因,它看起来像是有空格问题。我已设法将其减少为以下内容:
use Modern::Perl;
use v5.16;
use Regexp::Grammars;
use Data::Dumper;
my $grammar = qr{
<foo> <baz> | my <foo> is <baz>
<rule: foo> foo | fu | phoo
<rule: baz> bazz?
}ix;
while (<>) {
chomp;
if (/$grammar/) {
say Dumper(\%/);
}
else {
say "NO MATCH!!\n";
}
}
当程序运行时和任何匹配序列如
foo baz
phoo bazz
my fu is baz
进入程序返回
NO MATCH!!
但是,如果我在语法定义之前插入一个调试指令:
<debug: match>
<foo> <baz> | my <foo> is <baz>
...
我得到了我所期望的:
perl.exe : ========> Trying <grammar> from position 0
At line:1 char:5
+ perl <<<< .\test_grammar2.pl 2>&1 > output.txt
+ CategoryInfo : NotSpecified: (========> Tryin...from position 0:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
phoo bazz |...Trying <foo>
| |...Trying subpattern /foo/
| | \FAIL subpattern /foo/
| |...Trying next alternative
| |...Trying subpattern /fu/
| | \FAIL subpattern /fu/
| |...Trying next alternative
| |...Trying subpattern /phoo/
bazz | | \_____subpattern /phoo/ matched 'phoo'
| \_____<foo> matched 'phoo'
|...Trying <baz>
| |...Trying subpattern /bazz?/
[eos] | | \_____subpattern /bazz?/ matched 'bazz'
| \_____<baz> matched ' bazz'
\_____<grammar> matched 'phoo bazz'
$VAR1 = {
'' => 'phoo baz',
baz => ' bazz',
foo => 'phoo'
};
类似地,如果我在子规则和文字调用之间放置一个可选的空白序列:
<foo>\s*<baz> ...
...
我也得到了匹配。
我使用的是 Winodws 7、ActivePerl Build 1603、Perl 5.16.3 和 PowerShell。我也尝试过使用 cmd.exe 以防出现一些模糊的 PowerShell 问题,但我遇到了同样的问题。我也试过直接匹配:
my $s = q(fu baz);
if ($s =~ $grammar) {
...
}
但我遇到了同样的问题——使用相同的解决方案。
编辑:我学到了什么。
当使用 Regexp::Grammars 模块时,如果您的语法需要文字、子规则或两者之间有空格,那么您需要封装:
<foobaz>
<rule: foobaz> <foo> <baz> | my <foo> is <baz>
转义:
<foo>\ <baz> | my\ <foo>\ is\ <baz>
或插入空白序列:
<foo>\s+<baz> | my\s+<foo>\s+is\s+<baz>
最佳答案
好的,我知道问题出在哪里了。 Regexp::Grammars 表达式中的顶级匹配以标记模式(不忽略空格)而不是规则模式(忽略空格)处理。所以,要得到你想要的,你只需要添加一条顶级规则:
my $grammar = qr{
<top>
<rule: top> <foo> <baz> |
my <foo> is <baz>
<rule: foo> foo | fu | phoo
<rule: baz> bazz?
}ix;
这是我的完整程序:
use Modern::Perl;
use v5.16;
use Regexp::Grammars;
use Data::Dumper;
my $grammar = qr{
<top>
<rule: top> <foo> <baz> |
my <foo> is <baz>
<rule: foo> foo | fu | phoo
<rule: baz> bazz?
}ix;
1;
while (<>) {
chomp;
if (/$grammar/) {
say Dumper(\%/);
}
else {
say "NO MATCH!!\n";
}
}
这是我的输出:
% echo FU baz | perl grammar.pl
$VAR1 = {
'' => 'FU baz',
'top' => {
'' => 'FU baz',
'baz' => 'baz',
'foo' => 'FU'
}
};
% echo my phoo is bazz | perl grammar.pl
$VAR1 = {
'' => 'my phoo is bazz',
'top' => {
'' => 'my phoo is bazz',
'baz' => 'bazz',
'foo' => 'phoo'
}
};
Regexp::Grammars 的文档特别指出顶层是在 token 模式下完成的。添加顶级标记只会向解析树添加一层,但如果要在顶级忽略空格,我认为您没有选择。
关于regex - 在 Regexp::Grammars 模块中处理空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165273/
我对这里的区别是什么以及为什么一个有效而另一个无效感到困惑。谁能解释一下? //The string to search through var str = "This is a string /*
我很困惑这里有什么区别以及为什么一个有效而另一个无效。有人能解释一下吗? //The string to search through var str = "This is a string /* w
概述 RegExp 的构造函数创建了一个正则表达式对象,用模式来匹配文本。 有关正则表达式介绍,请阅读JavaScript指南中的正则表达式章节。 语法 文字和构造符号是可能的: /patt
在我的数据库中,我有一个公司表。该表有一个名为 tags 的字段,其中包含以下内容: Furniture Retail E-commerce B2C Home & Furniture Consumer
var str='The_Andy_Griffith_Show'; // string to perform replace on var regExp1=/\s|[A-Z]/g; var reg
我正在为 VBA 编写一个脚本,用于 Outlook 2013,它使用正则表达式,我发现的每个示例似乎都使用 Set regex = New RegExp创建一个 RegExp 对象。当我尝试这个时,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 4 年前。
在“How do I removing URLs from text?”中建议使用以下代码: require 'uri' #... schemes_regex = /^(?:#{ URI.scheme
所以我有一个 RegExp regex =/asd/ 我将它作为 key 存储在我的键值对存储系统中。 所以我说 str = String(regex) 返回 "/asd/"。 现在我需要将该字符串转
谁能提供一些例子来解释 regexp.Compile 之间的区别?和 regexp.CompilePOSIX ?我阅读了文档。但是我无法得到直观的理解。 最佳答案 Perl 和 POSIX 兼容的正则
我目前正在学习 SQL 并使用 SSMS 2017。我不明白为什么在使用 REGEXP 语法时出现错误,它似乎适用于其他任何人: SELECT * FROM List WHERE Name REGEX
我有一个包含文本的 emacs 缓冲区 a1b2c3 使用正则表达式构建器,我创建了正则表达式 "b\\(2\\)" 并且可以看到匹配突出显示(b2,2 的颜色不同)。 但是,当我将表达式粘贴到 re
这个问题已经有答案了: JavaScript: using constructor without operator 'new' (2 个回答) 已关闭 7 年前。 RegExp('hi') 和有什么
我的正则表达式是这样的: ((?:[a-z][a-z0-9_]*)).*?(\d+).*?((?:[a-z][a-z0-9_]*)).*?(\d+).*?([a-z]) 如果我将其作为 MySQL R
我对基准测试和功能感兴趣?是否有理由使用 Jakarta 正则表达式? 最佳答案 似乎没有什么理由。但是除了 Jakarta 图书馆之外,还有其他一些有趣的图书馆。此链接提供了一些有关性能和 perl
如果声明了 SPEC Env,我将尝试有条件地加载我的测试: var context = null if (process.env.SPEC) { context = require.contex
我尝试为 emacs 编写一些新的对齐规则,但发现这种奇怪且不一致的行为。当前缓冲区内容: "some thing" like => this hello => world and => aga
这个问题在这里已经有了答案: Is gcc 4.8 or earlier buggy about regular expressions? (3 个答案) 关闭 8 年前。 我尝试使用 C++11
我遇到了两个错误,都与编码有关并且都相关。 我在启动 WEBrick 时遇到的第一个错误(技术上是警告): /Users/USERNAME/example/config/initializers/bb
我有一个 almost-json文件。估计1000 行。这是其中的一部分: level: { 1: { cost: 200,
我是一名优秀的程序员,十分优秀!