Perl Regex ..简单的 if 语句
我正在研究 Perl 函数(或者我应该说子例程?)并且对 Perl(和正则表达式)相对较新。
除了一个简单的部分之外,整个事情都是有效的……一个需要使用正则表达式的 if 语句。
我需要检查一个字符串:是否
- 以可选的空格开头
- ,之后的空格包含 0 或 1 个用方括号括起来的字符串实例。(即 [test])
我编写的正则表达式是 ^\s*{\[$testString\]}?$
这不起作用。但是,我根本无法让它与正则表达式一起使用,所以我想知道我是否做得正确。
现在,我正在使用的测试用例,我希望 if 为 true 的字符串是 [fallback_test]
。
当我
if ($string eq ' [fallback_test]')
这样做时,它就起作用了。但是,如果我
if ($string =~ /fallback_test/)
这样做,它就不会像它应该的那样进入 if 块。理想情况下,它将使用我编写的正则表达式:
^\s*{\[$testString\]}?$
所以我的问题是......
- 为什么正则表达式对我不起作用?
- 当我让正则表达式在该块中正常工作时,该正则表达式是否会执行我想要的操作?
I'm working on a Perl function (or should I say sub-routine?) and am relatively new to Perl (and regular expressions).
The whole thing works except for one simple part... an if statement that needs to use a regular expression.
I need to check if a string:
- begins with optional white-space
- after the white-space contains 0 or 1 instances of a string enclosed in square brackets.. (i.e. [test])
The regular expression I've written is ^\s*{\[$testString\]}?$
This is not working. However, I haven't been able to get this to work with regular expressions at all, so I'm wondering if I'm doing it correctly.
Right now the test case I'm working with, the string I want the if to be true on is [fallback_test]
.
When I do
if ($string eq ' [fallback_test]')
then it works. However, if I do
if ($string =~ /fallback_test/)
then it doesn't enter into the if block like it should. Ideally, it will use the regular expression I've written:
^\s*{\[$testString\]}?$
So my questions are...
- Why are the regular expressions not working for me?
- Is that regular expression going to do what I want when I get the regex's to work properly in this block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您有类似的内容:
my $testString = "fallback_test";
尝试:
/^\s*\[${testString}\]?$/
Assuming you have something like:
my $testString = "fallback_test";
Try:
/^\s*\[${testString}\]?$/
您需要对可选字符串进行分组。这是工作示例:
这是要测试的 perl oneliner:
perl -e 'die“不匹配!” except " [fallback_test]" =~ /^\s*(?:\[.*?\])?$/'
说明:
在正则表达式
(subpattern)
中对子模式进行分组并存储$1..$9 变量中的匹配结果。(?:subpattern)
对子模式进行分组,但不将匹配结果存储在任何地方。.*?
将匹配第一个“]”字符之前的所有字符。您可能需要将其替换为您的测试字符串,例如“fallback_test”或“test”,或者一些正则表达式来满足您的要求。另请注意,此正则表达式将匹配空行。
You need to group optional string. Here's working example:
And here's perl oneliner to test:
perl -e 'die "not matched!" unless " [fallback_test]" =~ /^\s*(?:\[.*?\])?$/'
Explanation:
In regexp
(subpattern)
groups subpattern and stores matched result in $1..$9 variables.(?:subpattern)
groups subpattern, but not stores matched result nowhere..*?
will match all characters till first ']' character. You may want to replace this with your test string like 'fallback_test' or 'test', or some regular expression you to satisfy you requirements.Also note that this regexp will match an empty line.
所以需要分组而不是{}?,使用()?。
FWIW 你的正则表达式只会匹配 4 个不同的组合,因为你已经锚定了
边界线上有 ^$ 并有 2 个可选子表达式。
1 '' 空字符串
2''全是空格
3 '[string]' 只是 [string]
4 ' [string]' 空格后跟 [string]
如果这只是在单个正则表达式中发现的事实,那么您始终可以捕获两者
子表达式并稍后测试对您有意义的结果。
例如:
编辑 将 quotemeta 添加到正则表达式
if ($str =~ /^(\s*)(\[\Q$testString\E\])?$/) {
..测试 $2 的字符串
..测试 $1 的前导空格
但如果需要 [string],则更好的正则表达式将是
/^(\s*)\[\Q$testString\E\]$/
,然后测试 $1 是否包含可选空格。So a grouping is needed instead of {}?, use ()?.
FWIW your regex will only match 4 distinct combinations since you have anchored the
line on boundries with ^$ and have 2 optional subexpressions.
1 '' the empty string
2 ' ' all whitespaces
3 '[string]' just [string]
4 ' [string]' whitespaces followed by [string]
If this is just fact finding in a single regex, you can always capture both
subexpressions and test later for the result that is meaningfull to you.
For example:
edit adding quotemeta to regex
if ($str =~ /^(\s*)(\[\Q$testString\E\])?$/) {
..test $2 for the string
..test $1 for leading spaces
But if [string] is required, a better regex would be
/^(\s*)\[\Q$testString\E\]$/
, then test $1 if it contains optional whitespace.为什么要在正则表达式中添加大括号?它们用于说明重复次数,例如
/\d{5}/
。要执行您想要的操作,它应该类似于:
\Q...\E
当可以在正则表达式中解释元字符时,应该可以使我们避免运行时错误。请考虑到您要求的正则表达式也将匹配空行,除非您是故意的。
Why are you adding curly brackets in your regex? they are used for stating the number of repetition e.g.,
/\d{5}/
.To do what you want it should be like:
\Q...\E
should save us from runtime errors when a metacharacter could be interpreted in the regex.Take into account that the regex you are asking for will match empty lines too unless you mean it.