Regex 神秘的perl表达式

Regex 神秘的perl表达式,regex,perl,pdl,Regex,Perl,Pdl,我在perl(实际上是PDL)程序中发现以下语句: 有人能帮我这个perl编程的学徒解码吗 /\/([\w]+)$/i; 它是一个正则表达式,如果它是一个完整的语句,它将应用于$\uu变量,如下所示: $_ =~ /\/([\w]+)$/i; 它查找斜杠\/,后跟字母数字字符串\w+,后跟行尾$。它还捕获字母数字字符串(),该字符串在变量$1中结束。结尾的/i使其不区分大小写,在本例中无效。这是将$\u与后跟一个或多个字符的斜杠(不区分大小写)进行比较,并将其存储在$1 $_ value

我在perl(实际上是PDL)程序中发现以下语句:

有人能帮我这个perl编程的学徒解码吗

/\/([\w]+)$/i;
它是一个正则表达式,如果它是一个完整的语句,它将应用于
$\uu
变量,如下所示:

$_ =~ /\/([\w]+)$/i;

它查找斜杠
\/
,后跟字母数字字符串
\w+
,后跟行尾
$
。它还捕获字母数字字符串
()
,该字符串在变量
$1
中结束。结尾的
/i
使其不区分大小写,在本例中无效。

这是将
$\u
与后跟一个或多个字符的斜杠(不区分大小写)进行比较,并将其存储在
$1

$_ value     then     $1 value 
------------------------------
"/abcdes"     |       "abcdes"
"foo/bar2"    |       "bar2"
"foobar"      |       undef      # no slash so doesn't match

当然,我会从里到外解释:

\w
-匹配可在单词中使用的字符(字母数字加“\u1”)

[…]
-匹配

[\w]
-匹配可在单词中使用的单个字符(这里有点多余)

+
-尽可能匹配上一个字符,但必须至少出现一次

[\w]+
-多次匹配一组单词字符。这将找到一个词

(…)
-。请记住这组字符,以便以后使用

([\w]+)
-匹配一个单词,并记住它以备将来使用

$
-。在一行的末尾匹配某物

([\w]+)$
-匹配一行中的最后一个单词,并记住它以备以后使用

\/
-单个斜杠字符“/”。它必须用反斜杠转义,因为斜杠是特殊的

\/([\w]+)$
-在斜杠“/”后匹配一行中的最后一个单词,并记住该单词以备将来使用。这可能是从路径中获取目录/文件名

/…/
-语法

/…/i
-我的意思是

现在大家一起:

/\/([\w]+)$/i-匹配一行中的最后一个单词,并记住它以备将来使用;这个词必须在斜杠后面。基本上,从绝对路径获取文件名。不区分大小写的部分不相关,
\w
将已经匹配这两种情况

有关Perl正则表达式的更多详细信息,请参见:

正如JRGUSON所指出的,它对于标记正则表达式和解释各个部分非常有用。

您会发现该模块值得安装

#!/usr/bin/env perl
use YAPE::Regex::Explain;
#...may need to single quote $ARGV[0] for the shell...
print YAPE::Regex::Explain->new( $ARGV[0] )->explain;
假设此脚本名为“Rexplane”,请执行以下操作:

$ ./rexplain '/\/([\w]+)$/i'
…获得:

The regular expression:

(?-imsx:/\/([\w]+)$/i)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [\w]+                    any character of: word characters (a-z,
                             A-Z, 0-9, _) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [\w]+                    any character of: word characters (a-z,
                             A-Z, 0-9, _) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
  /i                       '/i'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
更新:

另见:。如模块文档中所述:

不支持Perl 5.6版之后添加的正则表达式语法,尤其是任何 5.10中添加的构造

这个问题值得一提。下面是一个例子来解释正则表达式的含义,并粘贴在这里以供记录

顺序:按顺序匹配以下所有内容
虽然它不能帮助“解释”正则表达式,但一旦你有了一个测试用例,Damian的新工具是一个很酷的工具,可以观察匹配过程中实际发生了什么。安装它,然后在命令行执行
rxrx
以启动调试器,然后键入
/\/([\w]+)$/
'/r'
(例如),最后键入
m
以启动匹配。然后可以通过反复按enter键逐步完成调试器。真的很酷

谢谢Tim…非常好的描述,完全符合代码的上下文。看来我需要更多的正则表达式经验后,40多年的编程<代码>$1
是未定义的,除非以前的匹配,在这种情况下,其值不变。
The regular expression:

(?-imsx:/\/([\w]+)$/i)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [\w]+                    any character of: word characters (a-z,
                             A-Z, 0-9, _) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  \/                       '/'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [\w]+                    any character of: word characters (a-z,
                             A-Z, 0-9, _) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
  /i                       '/i'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
/                                                  (slash)
                                               --+
Repeat                                           | (in GroupNumber:1)
   AnyCharIn[ WordCharacter] one or more times   |
                                               --+
EndOfLine