English 中文(简体)
• 如何在Perl获得一种完美的分类模式?
原标题:How to get a perfect match for a regexp pattern in Perl?
  • 时间:2011-10-11 14:54:24
  •  标签:
  • perl

www.un.org/Depts/DGACM/index_spanish.htm I ve tocompet a regularexpression, Deposit in aerra:

#!/bin/env perl

use warnings;
use strict;
my $expr = qr/s*(w+([d+])?)s+(w+([d+])?)/sx;
$str = "abcd[3] xyzg[4:0]";
if ($str =~ m/$expr/) {
    print "
%%%%%%%%% $`-----$&-----$ 
";
}
else {
    print "
********* NOT MATCHED
";
}

但是,我不停在美元和纸浆中;

%%%%%%%%% -----abcd[3] xyzg-----[4:0]

But expecting, it shouldn t go inside the if clause. What is intended is:

if $str = "abcd xyzg" => %%%%%%%%% -----abcd xyzg-----            (CORRECT)
if $str = "abcd[2] xyzg" => %%%%%%%%% -----abcd[2] xyzg-----      (CORRECT)
if $str = "abcd[2] xyzg[3] => %%%%%%%%% -----abcd[2] xyzg[3]----- (CORRECT)
if $str = "abcd[2:0] xyzg[3] => ********* NOT MATCHED             (CORRECT)
if $str = "abcd[2:0] xyzg[3:0] => ********* NOT MATCHED           (CORRECT)
if $str = "abcd[2] xyzg[3:0]" => ********* NOT MATCHED            (CORRECT/INTENDED)

but output is %%%%%%%%% -----abcd[2] xyzg-----[3:0] (WRONG) OR better to say this is not intended. In this case, it should/my_expectation go to the else block. Even I don t know, why $& take a portion of the string (abcd[2] xyzg), and $ having [3:0]? HOW? It should match the full, not a part like the above. If it didn t, it shouldn t go to the if clause.

Can anyone please help me to change my $expr pattern, so that I can have what is intended?

问题回答

By default, Perl regexes only look for a matching substring of the given string. In order to force comparison against the entire string, you need to indicate that the regex begins at the beginning of the string and ends at the end by using ^ and $:

my $expr = qr/^s*(w+([d+])?)s+(w+([d+])?)$/;

(Also,没有理由有/x modifier,因为您的舱面不包括任何字面的白色空间或# natures,而且没有理由使用/s modifier,因为你没有使用

EDIT:如果你不想让监管机构与整个扼杀物相对应,但你想要拒绝在配对部分后面加上“[0:0]”的东西,最简单的办法是使用头盔:

my $expr = qr/^s*(w+([d+])?)s+(w+([d+]|(?=[^[w])|$ ))/x;

这将与采取以下形式的任何事情相匹配:

  • beginning of the string (which your example in the comments seems to imply you want)
  • zero or more whitespace characters
  • one or more word characters
  • optional: [, one or more digits, ]
  • one or more whitespace characters
  • one or more word characters
  • one of the following, in descending order of preference:
      • [, one or more digits, ]
      • an empty string followed by (but not including!) a character that is neither [ nor a word character (The exclusion of word characters is to keep the regex engine from succeeding on "a[0] bc[1:2]" by only matching "a[0] b".)
      • end of string (A space is needed after the $ to keep it from merging with the following ) to form the name of a special variable, and this entails the reintroduction of the /x option.)

你们是否有需要满足的更无遗的要求?

The short answer is your regexp is wrong.
We can t fix it for you without you explaining what you need exactly, and the community is not going to write a regexp exactly for your purpose because that s just too localized a question that only helps you this one time.

You need to ask something more general about regexps that we can explain to you, that will help you fix your regexp, and help others fix theirs.

在你重新检查贵格时,我要作一般性回答。 使用一种控制工具,如regex buddy

So I m going to give a specific answer about what you re overlooking here:
Let s make this example smaller: Your pattern is a(bc+d)?. It will match: abcd abccd etc. While it will not match bcd nor bzd in the case of abzd it will match as matching only a because the whole group of bc+d is optional. Similarly it will match abcbcd as a dropping the whole optional group that couldn t be matched (at the second b).

试采会将像它们能够做到的那样,在它们能够与一些东西相匹配并满足整个模式时,与它们恢复真正的匹配。 如果你作出选择,他们只有在在场和相匹配时才会离开。

Here s what you tried:
qr/s*(w+([d+])?)s+(w+([d+])?)/sx
First, s and x aren t needed modifiers here.
Second, this regex can match:
Any or no whitespace followed by
a word of at least one alpha character followed by
optionally a grouped square bracketed number with at least one digit (eg [0] or [9999]) followed by at least one white space followed by
a word of at least one alpha character followed by
optionally a square bracketed number with at least one digit.

显然,当你要求其匹配<代码>abcd[0]xyzg[0:4]时。 上校终止了<代码>d+,但不符合>>的代码,从而支持整个集团,然后习惯认为该集团是任择的。 因此,通过不与最后的选任群体相匹配,你的模式是成功的。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签