0

I have a web application that takes input from a user, usually in the form of a filepath, hyperlink, or fileshare, but not always. A user may enter "\my.fileshare.com", "http://www.msdn.com", or "In my file cabinent". These inputs are exported to a Excel file. However, if the input is in the form of "\look on my desk" or "http://here it is" (notice the spaces), after the file is exported, and opened, Excel raises the ever so descriptive error message of, and I quote, "Error".

I'm adding to the existing code a regular expression validator to the textbox the user enters and edits these locations in. Because there are a large number of existing entries, the validator needs to be specific as possible, and only toss out the inputs that cause the Excel export to break. For example "\Will Work" will work, as will "Will Work", and "\This\will also work". I need a regular expression that if the string starts with \, http://, https://, ftp://, ftps://, the server or fileshare name does not have a space in it, and if it does not start with the \, http://, https://, ftp://, ftps://, its fine regardless.

I've been able to write the first part ^(\\)[^ \]+(\.)$|^(((ht|f)tp(s)?)://)[^ /]+(/.)$

but I can't figure out how to say ignore everything if it does not start with \, http://, https://, ftp://, ftps://.

4

2 に答える 2

1
 ^(?:(?:\\|(?:ht|f)tps?://)\S+|(?!\\|(?:ht|f)tps?://).*)$

説明:

^ # 文字列の開始
  (?: # 非キャプチャ グループの開始
    (?:\\|(?:ht|f)tps?://)\S+ # "\, http, ftp" の後にスペースなし
    | | # また
    (?!\\|(?:ht|f)tps?://).* # "\, http, ftp" の後に何も続かない
  ) # 非キャプチャ グループの終了
$ # 文字列の終わり

これは、エスケープされていない純粋な正規表現です。環境のルールに従って文字エスケープを追加します。

于 2009-08-07T13:08:09.173 に答える
0

編集:おっと時期尚早。

この表現では、「 http://www.google.com/hello world 」はまだ許可されていません:/

三度目の編集

どうぞ!

^(?:(?:\\|(?:ht|f)tps?://)[^ /\]+([/\]. )?|(?!\\|(?:ht|f )tps?://). )$

于 2009-08-07T19:13:06.793 に答える