6

I am a bit of regex newbie and I can't figure out how to set up a regular expression for this pattern I am trying to do.

The expression is meant to be in a Python pre-commit script and it will run a pre-commit hook if the files being commits match it.

My example list of files

vars/prod-region1/mysql.yml
vars/prod-region1/keys.yml
vars/prod-region1/test.yml
vars/stage-region2/mysql.yml
vars/stage-region2/keys.yml
vars/stage-region2/test.yml
vars/local/mysql.yml
vars/local/test.yml

I need a regex pattern that will match files that fall in the following directory pattern

  • vars/prod*/mysql.yml
  • vars/prod*/keys.yml
  • vars/stage*/mysql.yml
  • vars/stage*/keys.yml

my effort at the moment is

vars/(prod*|stage*)/(mysql|keys).yml

which is severely wrong. Any help would be great.

DavidG
  • 24,279
  • 14
  • 89
  • 82
MechaStorm
  • 1,432
  • 3
  • 17
  • 29
  • Fantastic question by the way. Keep it up. As for your expression it is the following: `vars\/(prod|stage).*?\/(mysql|keys)\.yml` – d0nut Nov 10 '15 at 01:51

1 Answers1

4

You're using * incorrectly. It is a repetition indicator, not a multi-character wildcard; there is no generic (variable length) wildcard in regular expression syntax. There are, however, single-character wildcards and quasi-wildcards. So, for example, .* matches any character, zero or more times. \S* matches zero or more non-whitespace characters. In your example, however, d* matches zero or more ds in a row. Similarly with e*.

You need to use something to match the additional characters. In this case, you want non-whitespace characters, zero or more times, so you should use \S*. You also should escape the / characters. And when you mean a literal ., not a wildcard character, you need to escape ., as well.:

vars\/(prod\S*|stage\S*)\/(mysql|keys)\.yml

Here's a demo.

elixenide
  • 44,308
  • 16
  • 74
  • 100