I need a regular expression that should allow files only with .txt
or with no extension.
For example: c:\\document\\assignment\\test.txt
or c:\\document\\assignment\\testing
This is what I have thus far:
^[a-zA-Z]:(\\\\[a-zA-Z0-9\\p{Space}[\\p{Punct}&&[^\\Q\\/:*?\"<>|\\E]]\\(\\)]+[^\\.])+(?=(\\.txt|\\.TXT))(\\.txt|\\.TXT)$
The above is working only for the .txt
but not for files with no extension.
The regular expression requires the case-insensitive flag, and should work with most regex engine flavors.
^(?:[a-z]:|\\|\.{1,2})(?:\\[^<>:"/\\|?*.]+)+(?:\.txt)?$
The regular expression produces a match by matching the following sub patterns.
.txt
You can simplify the expression if you don't need unc or relative paths by changing to first capture group to just az
. You can add more extensions by changing the last capture group. For example, (?:\\.(txt|exe|com)?
would match .txt
, .exe
, or .com
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.