Regular Expression for Valid Filename

Regular expression for valid filename

This is the correct expression:

string regex = @"^[\w\-. ]+$";

\w is equivalent of [0-9a-zA-Z_].

How to validate file name and file extension from single Regex?

Something like this?

return /^[a-z0-9_.@()-]+\.txt$/i.test(fileName);

If you want two separate checks, are you indicate in comments, you might do something like this:

var validFilename = /^[a-z0-9_.@()-]+\.[^.]+$/i.test(fileName);
var validExtension = /\.txt$/i.test(fileName);

The first expression tests everything up until the last period, and verifies that there is at least one non-period character after the last period, comprising the extension.

If you don't want to bother with the definition of what is a filename, you may instead invert the test, and explicitly check for the presence of invalid characters (here I'm testing the entire string again, so validFilename will be false even if you have invalid characters only in the extension - one way around that would be to validate extension before filename)

var validFilename = !/[^a-z0-9_.@()-]/i.test(fileName);

Javascript regex for validating filenames

You need to add a starting anchor:

/^[0-9a-zA-Z ... ]+$/

This tells the engine to match from the start of the input all the way to the end of the input, whereas for your original expression it only needs to match at the end of the input.

How to validate a filename using Regex?

The following should suffice:

"(?i)abc[12]_def\d{6,7}_ghi[123]\.xml

Note the use of ?i flag to match case-insensitively. You may also use the Pattern.CASE_INSENSITIVE constant.

This regex matches abc followed by either a 1 or a 2, followed by _def, followed by 6-7 digits, followed by _ghi, followed by a 1, 2, or 3, with the extension .xml.

Demo

Regex for finding valid filename

Here you go:

"[^/?*:;{}\\]+\\.[^/?*:;{}\\]+"

"One or more characters that aren't any of these ones, then a dot, then some more characters that aren't these ones."

(As long as you're sure that the dot is really required - if not, it's simply: "[^/?*:;{}\\]+"

I need to validate filename and extension in javascript

You misused character classes ([...] that match 1 symbol) for grouping construct ((...) that are used to group alternatives of sequences of symbols). Also, you declare the . as optional with ?, but it should not (acc. to your specifications). Also, 3 or more means you need a {3,} limiting quantifier, not a {3} (that matches exactly 3 occurrences of the quantified subpattern).

Use

/^[A-Za-z]{3,}\.(?:pdf|doc|html)$/

See the regex demo.

Or, to make the whole pattern case insensitive:

/^[a-z]{3,}\.(?:pdf|doc|html)$/i
^

Pattern details:

  • ^ - start of string
  • [A-Za-z]{3,} - 3 or more ASCII letters
  • \. - a literal dot
  • (?:pdf|doc|html) - a non-capturing group matching either pdf, doc, or html sequence of characters
  • $ - end of string

Regex to validate a filename

If your idea is only to exclude ilegal and space char you can use something like:

'^[^*&%\s]+$'

where you can add any "ilegal" char into the list of chars (in this case it ignores *, &, % and space) \s is the space! The ^ inside the [] is part of the regex syntax it means: do not match any chars inside [].

C regex validate filename under a folder

POSIX offer a nice function realpath()

realpath() expands all symbolic links and resolves references to /./,
/../ and extra '/' characters in the null-terminated string named by
path to produce a canonicalized absolute pathname. The resulting
pathname is stored as a null-terminated string, up to a maximum of
PATH_MAX bytes, in the buffer pointed to by resolved_path. The
resulting path will have no symbolic link, /./ or /../ components.

If you can use it, I think it will fit your need, if not maybe you could copy the source code.

Regular expressions in C# for file name validation

As answered already, GetInvalidFileNameChars should do it for you, and you don't even need the overhead of regular expressions:

if (proposedFilename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
{
MessageBox.Show("The filename is invalid");
return;
}


Related Topics



Leave a reply



Submit