user6791424
user6791424

Reputation:

Match anything.c but not anything.in.c

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.

Upvotes: 1

Views: 73

Answers (2)

zzxyz
zzxyz

Reputation: 2981

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

Upvotes: 0

Sherpa
Sherpa

Reputation: 93

This worked for me.

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

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

*Edited do to good information from zzxyz

Upvotes: 3

Related Questions