0

Can anyone explain what is being done below?

 $name=~m,common/([^/]+)/run.*/([^/]+)/([^/]+)$,;
4
  • Testing to see if a string matches a regular expression but not doing anything with the result. Commented Nov 2, 2021 at 21:12
  • The code checks variable $name for a match against common/*/run*/*/*, in term of shell (bash, zsh, csh, ...), perhaps match against to some path in filesystem. It can be rewritten as $name =~ m!common/.*?/run.*?/.*?/.*?$!; Commented Nov 2, 2021 at 22:37
  • Re "common/*/run*/*/* in term of shell", I think that's what it was meant to do, but that's not what it does. .* can match strings containing /. /// @PolarBear Re "It can be rewritten as", All those ? are pointless. Your rewrite can match common/foo/run/bar/baz/moo/moo/moo/moo/moo/moo/moo/moo/moo. I don't think you intended that. Remember that ^/.*?/$ matches ///. Commented Nov 2, 2021 at 23:32
  • @ikegami Indeed I did not intended it, if I would use something of this kind in the code I would perhaps chosen other approach. One line of code does not say much about intentions of the programmer or the problem -- it is not clear what perl script supposed to do. Commented Nov 3, 2021 at 0:25

1 Answer 1

4
  • 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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.