Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
    
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 3:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.