Zazy
Zazy

Reputation: 97

perl regex - pattern matching

Can anyone explain what is being done below?

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

Upvotes: 0

Views: 167

Answers (1)

ikegami
ikegami

Reputation: 386676

  • 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.

Upvotes: 4

Related Questions