简体   繁体   English

perl 正则表达式 - 模式匹配

[英]perl regex - pattern matching

谁能解释下面正在做什么?

 $name=~m,common/([^/]+)/run.*/([^/]+)/([^/]+)$,;
  • common , run and / are match themselves. commonrun/是匹配自己的。
  • () captures. ()捕获。
  • [^/]+ matches 1 or more characters that aren't / . [^/]+匹配 1 个或多个不是/字符。
  • .* matches 0 or more characters that aren't Line Feeds. .*匹配 0 个或多个不是换行符的字符。 [1] [1]
  • $ is equivalent to ( \\n?\\z ). $等价于 ( \\n?\\z )。 [2] [2]
  • \\n optionally matches a Line Feed. \\n可选地匹配换行符。
  • \\z matches the end of the string. \\z匹配字符串的结尾。

I think it's trying to match a path of one or both of the following forms:我认为它试图匹配以下一种或两种形式的路径:

  • .../common/ XXX /runYYY/ XXX / XXX
  • common/ XXX /runYYY/ XXX / XXX

Where在哪里

  • XXX is a sequence of at least one character that doesn't contain / . XXX是至少一个不包含/字符的序列。
  • YYY is a sequence of any number of characters (incl zero) that doesn't contain / . YYY是不包含/的任意数量字符(包括零)的序列。

It matches more than that, however.然而,它比这更匹配。

  • It matches uncommon/ XXX /runYYY/ XXX / XXX它匹配uncommon/ XXX /runYYY/ XXX / XXX
  • It matches common/ XXX /runYYY/XXX/XXX/XXX/XXX/ XXX / XXX它匹配common/ XXX /runYYY/XXX/XXX/XXX/XXX/ XXX / XXX

The parts in bold are captured (available to the caller).粗体部分被捕获(可供调用者使用)。


  1. When the s flag isn't used.当不使用s标志时。
  2. When the m flag isn't used.当不使用m标志时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM