Reputation: 53
I'm trying to make a regex that matches anything except an exact ending string, in this case, the extension '.exe'.
Examples for a file named:
So far I created the regex /.*\.(?!exe$)[^.]*/
but it doesn't work for cases 1 and 6.
Upvotes: 4
Views: 3449
Reputation: 397
string pattern = @" (?x) (.* (?= \.exe$ )) | ((?=.*\.exe).*)";
.exe
. The condition is not included in the match. .exe
.(?x) is means that white spaces inside the pattern string are ignored. Or don't use (?x) and just delete all white spaces.
It works for all the 6 scenarios provided.
Upvotes: -1
Reputation: 18490
You can use a positive lookahead.
^.+?(?=\.exe$|$)
^
start of string.+?
non greedily match one or more characters...(?=\.exe$|$)
until literal .exe
occurs at end. If not, match end.Upvotes: 2
Reputation:
Do you mean regex matching or capturing?
There may be a regex only answer, but it currently eludes me. Based on your test data and what you want to match, doing something like the following would cover both what you want to match and capture:
name = 'foo.bar.exe'
match = /(.*).exe$/.match(name)
if match == nil
# then this filename matches your conditions
print name
else
# otherwise match[1] is the capture - filename without .exe extension
print match[1]
end
Upvotes: 0