36

I have a regex I stuck in my .gitignore similar to:

(Big|Small)(State|City)-[0-9]*\.csv

It didn't work so I tested it against RegexLab.NET.

I then found the gitignore man page which led me to learn that gitignore doesn't use regexes, but rather fnmatch(3).

However, fnmatch it doesn't seem to have an equivalent of the capture groups. Is this feasible or do I need to break this into three lines?

Renan
  • 16,976
  • 8
  • 69
  • 88
Justin Dearing
  • 1,391
  • 3
  • 13
  • 26

1 Answers1

44

There's no way to express this regular expression with the patterns that gitignore supports. The problem is not the lack of capture groups (in fact, you are not using capture groups as such), the problem is the lack of a | operator. You need to break this into four lines.

BigState-[0-9]*.csv
SmallState-[0-9]*.csv
BigCity-[0-9]*.csv
SmallCity-[0-9]*.csv

Note that the patterns match e.g. BigState-4foo.csv, since * matches any sequence of characters. You can't do better than this with glob patterns, unless you're willing to match only a fixed number of digits.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 3
    I ended up doing exactly what you said. Also, thanks for pointing out * does not repeat the previous expression, but is a while card. Its good enough for my needs, but I was hoping for better. – Justin Dearing Feb 17 '12 at 03:01