Home > front end >  perl regex - pattern matching
perl regex - pattern matching

Time:11-04

Can anyone explain what is being done below?

 $name=~m,common/([^/] )/run.*/([^/] )/([^/] )$,;

CodePudding user response:

  • common, run and / are match themselves.
  • () captures.
  • [^/] matches 1 or more characters that aren't /.
  • .* matches 0 or more characters that aren't Line Feeds.[1]
  • $ is equivalent to (\n?\z).[2]
  • \n optionally matches a Line Feed.
  • \z matches the end of the string.

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 /.
  • YYY is a sequence of any number of characters (incl zero) that doesn't contain /.

It matches more than that, however.

  • It matches uncommon/XXX/runYYY/XXX/XXX
  • It matches 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.
  2. When the m flag isn't used.
  •  Tags:  
  • perl
  • Related