简体   繁体   中英

regex for ng-pattern for filepath

I have arrived at a regex for file path that has these conditions,

  • Must match regex ^(\\\\\\\\[^\\\\]+\\\\[^\\\\]+|https?://[^/]+) , so either something like \\server\\share (optionally followed by one or more "\\folder"s), or an HTTP(S) URL
  • Cannot contain any invalid path name chars( ",<,>, |)

How can i get a single regex to use in angular.js that meets these conditions

Ok, first the regex, than the explanation:

(?<folderorurl>(?<folder>(\\[^\\\s",<>|]+)+)|(?<url>https?:\/\/[^\s]+))

Your first condition is to match a folder name which must not contain any character from ",<>|" nor a whitespace. This is written as:

[^\s,<>|] # the caret negates the character class, meaning this must not be matched

Additionally, we want to match a folder name optionally followed by another (sub)folder, so we have to add a backslash to the character class:

[^\\\s,<>|] # added backslash

Now we want to match as many characters as possible but at minimum one, this is what the plus sign is for ( + ). With this in mind, consider the following string:

\server\folder

At the moment, only "server" is matched, so we need to prepend a backslash, thus "\\server" will be matched. Now, if you break a filepath down, it always consists of a backslash + somefoldername , so we need to match backslash + somefoldername unlimited times (but minimum one):

(\\[^\\\s",<>|]+)+

As this is getting somewhat unreadable, I have used a named capturing group ( (?<folder>) ):

(?<folder>(\\[^\\\s",<>|]+)+)

This will match everything like \\server or \\server\\folder\\subfolder\\subfolder and store it in the group called folder .

Now comes the URL part. A URL consists of http or https followed by a colon, two forward slashes and "something afterwards":

https?:\/\/[^\s]+ # something afterwards = .+, but no whitespaces

Following the explanation above this is stored in a named group called "url":

(?<folder>(\\[^\\\s",<>|]+)+)

Bear in mind though, that this will match even non-valid url strings (eg https://www.google.com.256357216423727... ), if this is ok for you, leave it, if not, you may want to have a look at this question here on SO .

Now, last but not least, let's combine the two elements with an or , store it in another named group (folderorurl) and we are done. Simple, right?

(?<folderorurl>(?<folder>(\\[^\\\s",<>|]+)+)|(?<url>https?:\/\/[^\s]+))

Now the folder or a URL can be found in the folderorurl group while still saving the parts in url or folder . Unfortunately, I do know nothing about angular.js but the regex will get you started. Additionally, see this regex101 demo for a working fiddle.

Your current regex doesn't seem to match what you want. But given it is correctly doing what you want, then this will add the negation :

^(?!.*[ "<>|])(\\\\[^\\]+\\[^\\]+|https?://[^/]+)

Here we added a negative lookahead to see if any characters are in the string which we will fail the match. If we find none, then the rest of the regular expression will continue.

If I understand your requirements correctly, you could probably do this :

^(?!.*[ "<>|])(\\\\|https?://).*$

This will still not match any invalid characters defined in the negative lookahead, and also meets your criteria of matching one or more path segments, as well as http(s) and is much simpler.

The caviate is that if you require 2 or more path segments, or a trailing slash on the url, than this will not work. This is what your regex seems to suggest.

So in that case this is still somewhat cleaner than the original

^(?!.*[ "<>|])(\\\\[^\\]+\\.|https?://[^/]+/).*$

One more point. You ask to match \\server\\share , yet your regex opens with \\\\\\\\ . I have assumed that \\server\\share should be \\\\server\\share and wrote the regex's accordingly. If this is not the case, then all instances of \\\\\\\\ in the examples I gave should be changed to \\\\

  • Must match regex ^(\\\\\\\\[^\\\\]+\\\\[^\\\\]+|https?://[^/]+) , so either something like \\\\server\\share (optionally followed by one or more "\\folder"s), or an HTTP(S) URL
  • Cannot contain any invalid path name chars( ",<,>, |)

To introduce the second condition in your regex, you mainly just have to include the invalid characters in the negated character sets, eg instead of [^/] use [^/"<>|] .

Here's a working example with a slightly rearranged regex:

 paths = [ '\\\\server\\\\share', '\\\\\\\\server\\\\share', '\\\\\\\\server\\\\share\\\\folder', 'http://www.invalid.de', 'https://example.com', '\\\\\\\\<server\\\\share', 'https://"host.com', '\\\\\\\\server"\\\\share', ] for (i in paths) { document.body.appendChild(document.createTextNode(paths[i]+' '+ /^\\\\(\\\\[^\\\\"<>|]+){2,}$|^https?:\\/\\/[^/"<>|]+$/.test(paths[i]))) document.body.appendChild(document.createElement('br')) } 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM