Click here to Skip to main content
15,033,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need a regular Expression to check a string (FileName)

1. it can contain only numbers.
2. it can contain only Alphabetes.
3. it can contain only Underscore.
4. it can contain Combination of numbers,alphabets,Underscore,space,dash(-).
5. it must contain only one or zero .(dot).
6. After .(dot) only Alphabetes and Numbers are allowed.
7. Lenght of string should not be more than 200 character.
8. Must not contain special character or any other thing.
9. Must not contain space Only.
10. Must not contain dash only.
11. Must not contain more than one dot .

I have tried this

1. [^a-zA-Z 0-9_.-]

2. [^a-zA-Z 0-9_-]+(.[jJ][pP][gG]|.[gG][iI][fF])$

but not working.

it can except these strings -- 1.pdf ,a.pdf , abc.jpg, _.pdf , a1.jpg , 1a_.pdf , 1 a-.pdf , abcd .

Should not accept these strings -- abc.phb.pdf , abc.phb.jpg , a%im.jpg ,..jpg , ..pdf , .pdf, .jpg , -.pdf .
Posted
Updated 12-Feb-16 13:56pm
v2

Try:
^([A-Za-z0-9\-_][A-Za-z0-9\-_\s]*)(\.[A-Za-z0-9\-_]*)$

And check the total length manually outside the Regex.
   
Comments
Member9927612 23-Dec-15 7:15am
   
this is not working as it accept these string -- abc.phb.pdf , abc.phb.jpg , a%im.jpg ,..jpg , ..pdf , .pdf, .jpg , -.pdf .
OriginalGriff 23-Dec-15 7:24am
   
Not when I tested it with Expresso.
How did you test it?
Member9927612 23-Dec-15 7:37am
   
Public Shared Function IsValidFileName(ByVal strToCheck As String) As Boolean

Dim objAlphaNumericPattern As New Regex("^([A-Za-z0-9-_][A-Za-z0-9-_\s])(.[A-Za-z0-9-_])$")
Return objAlphaNumericPattern.IsMatch(strToCheck)
End Function

it will return false if not match
please correct me if i m wrong somwhere
OriginalGriff 23-Dec-15 7:57am
   
Ah! Not your fault (or mine, I'm afraid) - the regex I gave you has been corrupted by the Markdown processor... :sigh:
Let's see if I can post it here uncorrupted...

^([A-Za-z0-9\-_][A-Za-z0-9\-_\s]*)(\.[A-Za-z0-9\-_]*)$

Markdown stole a couple of backslashes, some stars...all of which are critical.
Member9927612 24-Dec-15 6:14am
   
Thanks a lot .it works perfect .
OriginalGriff 23-Dec-15 7:58am
   
Yeah, that looks better...
You cannot reasonably check all constraints with Regex.
I would first check coded for the cases 7,9,10, e.g. in this sequence (pseudo code):
all cases: string s = name.Trim();.
Case 9: if (s != name) --> error (leading/trailing spaces).
Case 10: if (s == "-") --> error (dash-only name).
Case 7: if (s.Length > 200) --> error(name too long).

Then you could check for the patterns. E.g. to make it localization aware, you need Unicode General Categories[^] instead of the plain 7 bit ASCII character sets.
Regex for cases 1-6, 8, 11: ^[-\w][-\s\w]*(?:[.][\p{L}\p{N}]+)?$
Cheers
Andi
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900