hsgu
hsgu

Reputation: 864

Regex match any string not containing dot character

for example match any folder name except files that have dot(.) before extension
I try [^\.] and .+[^\.].* nothing work

Upvotes: 21

Views: 43969

Answers (5)

Ry-
Ry-

Reputation: 225281

You need to anchor it:

^[^.]+$

That will match a string composed of any characters except for dots. Is that what you mean by "before extension"? If you mean "at the beginning", then ^[^.] will do the trick.

But if this isn't, say, grep or something, and you have an actual programming language, this might be better accomplished there. (And even with grep it’s better to write just grep -v '^\.', for example.)

Upvotes: 39

ntheorist
ntheorist

Reputation: 109

Don't bother with regex for that, which is expensive. Here's a faster example (in php)

foreach($files as $file)
{
    // ignore dot files
    if( 0 === strpos($file,'.') ) continue;
    ...
}

Upvotes: 0

codaddict
codaddict

Reputation: 455440

You can do:

^[^.]+$

or

^(?!.*\.).*$

Upvotes: 3

DWright
DWright

Reputation: 9500

Try ^[^.]+$. BTW, you don't need to escape dot inside [].

Upvotes: 7

Vishal Suthar
Vishal Suthar

Reputation: 17194

What about this:

^[^.]+$

Demo Regex

Upvotes: 3

Related Questions