1

I'm trying to write a regex that matches a.c, hello.c, etc.c, but not a.in.c, hello.in.c, etc.in.c.

Here's my regex: https://regex101.com/r/jC8nB0/21

It says that the negative lookahead won't match what I specified, that is, .in.c. I didn't know where to teach it to match .c. I tried both inside the parenthesis and outside.

I've read Regex: match everything but specific pattern but it talks about matching everything except something, but I need to match a rule except other rule.

halfer
  • 18,701
  • 13
  • 79
  • 158

2 Answers2

3

This worked for me.

.*(?<!(\.in))\.c

https://www.regular-expressions.info/lookaround.html

*Edited do to good information from zzxyz

Sherpa
  • 93
  • 6
  • this works well if your string is definitely a single filename and just a filename. Probably good for the op. Although it should probably be `\.in` to avoid `writein.c` from being filtered out. – zzxyz May 11 '18 at 01:07
0

This is actually a bit complicated given unknown input. The following isn't perfect, but it avoids .cpp files, and deals with strings that don't contain filenames, or longer strings that do.

\b\S+(?<!\.in)\.c\b

https://regex101.com/r/jC8nB0/286

zzxyz
  • 2,794
  • 1
  • 12
  • 31