-1

Javascript を .NET に変換しようとしていますが、完全に正しく変換できないようです。

Express for NodeJsで呼び出されるメソッドです。パス/test/:value1/:value2を URL の一部で使用できる正規表現に変換します。

/**
* Normalize the given path string,
* returning a regular expression.
*
* An empty array should be passed,
* which will contain the placeholder
* key names. For example "/user/:id" will
* then contain ["id"].
*
* @param {String|RegExp|Array} path
* @param {Array} keys
* @param {Boolean} sensitive
* @param {Boolean} strict
* @return {RegExp}
* @api private
*/

exports.pathRegexp = function(path, keys, sensitive, strict) {
  if (path instanceof RegExp) return path;
  if (Array.isArray(path)) path = '(' + path.join('|') + ')';
  path = path
    .concat(strict ? '' : '/?')
    .replace(/\/\(/g, '(?:/')
    .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){
      keys.push({ name: key, optional: !! optional });
      slash = slash || '';
      return ''
        + (optional ? '' : slash)
        + '(?:'
        + (optional ? slash : '')
        + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'
        + (optional || '')
        + (star ? '(/*)?' : '');
    })
    .replace(/([\/.])/g, '\\$1')
    .replace(/\*/g, '(.*)');
  return new RegExp('^' + path + '$', sensitive ? '' : 'i');
}

私はそれを次のように変換しようとしました:

private Regex ConvertPathToRegex(string path, bool strict, out List<string> keys) {

    keys = new List<string>();

    List<string> tempKeys = new List<string>();

    string tempPath = path;

    if (strict)
        tempPath += "/?";

    tempPath = Regex.Replace(tempPath, @"/\/\(", delegate(Match m) {
        return "(?:/";
    });

    tempPath = Regex.Replace(tempPath, @"/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?", delegate(Match m) {

        string slash = (!m.Groups[1].Success) ? "" : m.Groups[1].Value;
        bool formatSuccess = m.Groups[2].Success;
        string format = (!m.Groups[2].Success) ? "" : m.Groups[2].Value;
        string key = m.Groups[3].Value;
        bool captureSuccess = m.Groups[4].Success;
        string capture = m.Groups[4].Value;
        bool optional = m.Groups[5].Success;
        bool star = m.Groups[6].Success;

        tempKeys.Add(key);

        string expression = "/";
        expression += (optional ? "" : slash);
        expression += "(?:";
        expression += (optional ? slash : "");
        expression += (formatSuccess ? format : "");
        expression += (captureSuccess ? capture : (formatSuccess ? format + "([^/.]+?" : "([^/]+?)")) + ")";
        expression += (star ? "(/*)" : "");

        return expression;
    });

    tempPath = Regex.Replace(tempPath, @"/([\/.])", @"\$1");
    tempPath = Regex.Replace(tempPath, @"/\*", "(.*)");
    tempPath = "^" + tempPath + "$";

    keys.AddRange(tempKeys);

    return new Regex(tempPath, RegexOptions.Singleline | RegexOptions.IgnoreCase);
}

しかし問題は、この方法が正しく機能していないことです。私は正規表現のスーパースターではないので、これについて何か助けが得られるかどうか疑問に思っていました.

も追加すると、どういうわけかメソッドが台無しになります?param=1

編集:最初にパスからクエリパラメーターを削除すると、実際には非常にうまく機能しています。

申し訳ありませんが、質問に対する答えは、URL からクエリ パラメータを削除することでした。

4

1 に答える 1

0

私はこのポートをPHPで作成しました。おそらく、expressが何をするのかをよりよく理解するのに役立つかもしれません。それは、パラメーターを照合するための正規表現を返します。

<?php
public static function pathRegexp($path, array &$keys, $sensitive = false, $strict = false)
{
    if (is_array($path)) {
        $path = '(' . implode('|', $path) . ')';
    }

    $path = (($strict === true) ? $path : (rtrim($path, '/ ') . '/?'));
    $path = preg_replace('/\/\(/', '(?:/', $path);
    $path = preg_replace_callback('/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/', function($matches) use(&$keys) {
                $slash = (isset($matches[1]) ? $matches[1] : "");
                $format = (isset($matches[2]) ? $matches[2] : false);
                $key = (isset($matches[3]) ? $matches[3] : null);
                $capture = (isset($matches[4]) ? $matches[4] : false);
                $optional = (isset($matches[5]) ? $matches[5] : false);
                $star = (isset($matches[6]) ? $matches[6] : false);

                array_push($keys, array("name" => $key, "optional" => (!!$optional)));

                return ''
                        . ($optional ? '' : $slash)
                        . '(?:'
                        . ($optional ? $slash : '')
                        . ($format ? $format : '')
                        . ($capture ? $capture : ($format ? '([^/.]+?)' : '([^/]+?)')) . ')'
                        . ($optional ? $optional : '')
                        . ($star ? '(/*)?' : '');
            }, $path);
    $path = preg_replace('/([\/.])/', '\\/', $path);
    $path = preg_replace('/\*/', '(.*)', $path);
    $path = '/^' . $path . '$/' . ($sensitive ? '' : 'i');
    return $path;
}
于 2013-03-13T12:35:27.600 に答える