A String with an extension and a string without(Regex)

I want to look for a string without an extension, for example if I have:

mynameisjeff.jpeg
mynameisjohn.notanextension
mynameisbob

In the set of strings middle and bottom is the string I want, but the top is the string I want to ignore.

This is the regex expression I have at the moment:

(.+)[^\.]+

And it produces the following result and it seems to ignore the not bracket so it matches with

mynameisjeff.jpeg

which is the string i'm trying to aviod

Answers


This matches anything with no extension: /^[^.]+$/

This matches anything with something that looks like an extension: /^([^.]+)\.(.+)/ and puts the filename in the 1st group and the extension in the 2nd. You will need a searchable array of extensions that you'll need to test against.

There's a way to do it in one regex, if you really need that, but it gets very long and ugly if you have a lot of extensions.

This is maybe gets you part way:

if (/^[^.]+$/.test(filename)) {
    alert(filename+" has no extension");
} else {
    var regex = /^([^.]+)\.(.+)/;
    var matches = regex.exec(filename);
    alert("The file part is "+matches[1]);
    alert("The extension is "+matches[2]);
}

Even more:

function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

// testFilename returns true if filename does not match a known file type (i.e. has a recognized extension)
function testFilename(filename) {
    var extensions = [ "txt", "html", "foo", "bar" ];

    var foo = false;
    if (/^[^.]+$/.test(filename)) {
        foo = true;
    } else {
        var regex = /^([^.]+)\.(.+)/;
        var matches = regex.exec(filename);
        if (!contains(extensions, matches[2].toLowerCase())) {
            foo = true;
        }
    }
    return foo;
}

It can be done by one single regex by using negative lookahead:

^(?!.*\.(jpeg|jpg|ext|you|want|to|exclude)$)

(Tested using python)

>>> re.match(r'^(?!.*\.(jpeg|jpg)$)', "foo")
<_sre.SRE_Match object at 0x02216FA0>
(with Match object returned means it matches)

>>> re.match(r'^(?!.*\.(jpeg|jpg)$)', "foo.bar")
<_sre.SRE_Match object at 0x02224060>
(with Match object returned means it matches)

>>> re.match(r'^(?!.*\.(jpeg|jpg)$)', "foo.jpeg.bar")
<_sre.SRE_Match object at 0x02216FA0>
(with Match object returned means it matches)

>>> re.match(r'^(?!.*\.(jpeg|jpg)$)', "foo.jpeg")
>>>
(No Match object returned means it does not match)

If you want to split the string into "file-part" and "extension", so you can determine by code, or for further processing, it can be done using this regex:

^(.*?)(?:\.([^.]*))?$

This will treat the part before the last period as "file-part", and part after the last period as "extension". File part is in group 1 while extension is in group 2

E.g. (test in Python)

>>> re.match(r"^(.*?)(?:\.([^.]*))?$", "foo.asdfa.bar").groups()
('foo.asdfa', 'bar')

>>> re.match(r"^(.*?)(?:\.([^.]*))?$", "foo.asdfa.").groups()
('foo.asdfa', '')

>>> re.match(r"^(.*?)(?:\.([^.]*))?$", "foo").groups()
('foo', None)

Need Your Help

Is there a SQL technique for ordering by matching multiple criteria?

php sql mysql sorting

I have several tables that get JOINed together to form a table with columns

Eclipse MAT: remote jvm monitoring

eclipse eclipse-plugin jvm jmx eclipse-memory-analyzer

Is it possible, using that eclipse plugin, to remote connect to a running jvm (jmx enabled) as I would do with jConsole or jProfiler, for monitoring and debugging heap, threads and so on?