tags:

views:

47

answers:

2

I need a regex for finding all .h, .c, and .cpp files in a folder. Help?

[no, this isn't homework, I just don't have time to learn regexes right now]

+1  A: 

Try this:

/\.h$|\.c$|\.cpp$/

You can also write it like this:

/\.(h|c|cpp)$/
Mark Byers
+3  A: 

Try:

\.(h|c(pp)?)$

But do you really need a regex? Where are you using this?

Matthew Flaschen
You actually save 1 character by explicitly including "cpp" and it's slightly easier to read! I'd probably have done the same thing though, just because it's cooler.
Mark
@Mark, true, but consider what happens if c++ and cxx are added. ;)
Matthew Flaschen
Would this be cool enough? `\.(?:h|c(?:([px+])\1)?)$`
Amarghosh
Yes, but it's still 3 more than `\.(h|c(pp|xx|\+\+)?)$`, @Amarghosh :)
Matthew Flaschen
May be, but mine looks more cool (read obfuscated/hard to read) ;)
Amarghosh