Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用回调和正则表达式方法将JavaScript代码转换为C中的等效代码#_Javascript_C#_Regex_.net Core - Fatal编程技术网

使用回调和正则表达式方法将JavaScript代码转换为C中的等效代码#

使用回调和正则表达式方法将JavaScript代码转换为C中的等效代码#,javascript,c#,regex,.net-core,Javascript,C#,Regex,.net Core,我有下面的JavaScript代码,它返回了一个与user命令相关的函数回调,user命令可以以不同的方式使用,因此需要RegEx: (function (undefined) { "use strict"; var root = this; var commandsList = []; var debugStyle = 'font-weight: bold; color: #00f;'; // The command matching code is a modifie

我有下面的JavaScript代码,它返回了一个与user命令相关的函数回调,user命令可以以不同的方式使用,因此需要RegEx:

(function (undefined) {
  "use strict";

  var root = this;
  var commandsList = [];
  var debugStyle = 'font-weight: bold; color: #00f;';

  // The command matching code is a modified version of Backbone.Router by Jeremy Ashkenas, under the MIT license.
  var optionalParam = /\s*\((.*?)\)\s*/g;
  var optionalRegex = /(\(\?:[^)]+\))\?/g;
  var namedParam    = /(\(\?)?:\w+/g;
  var splatParam    = /\*\w+/g;
  var escapeRegExp  = /[\-{}\[\]+?.,\\\^$|#]/g;
  var commandToRegExp = function(command) {
    command = command.replace(escapeRegExp, '\\$&')
                  .replace(optionalParam, '(?:$1)?')
                  .replace(namedParam, function(match, optional) {
                    return optional ? match : '([^\\s]+)';
                  })
                  .replace(splatParam, '(.*?)')
                  .replace(optionalRegex, '\\s*$1?\\s*');
    return new RegExp('^' + command + '$', 'i');
  };

  var registerCommand = function(command, cb, phrase) {
    commandsList.push({ command: command, callback: cb, originalPhrase: phrase });
      root.console.log('Command successfully loaded: %c'+phrase, debugStyle);
  };

root.fonixListen = {
    addCommands: function(commands) {
      var cb;
      for (var phrase in commands) {
        if (commands.hasOwnProperty(phrase)) {
          cb = root[commands[phrase]] || commands[phrase];
          if (typeof cb === 'function') {
            // convert command to regex then register the command
            registerCommand(commandToRegExp(phrase), cb, phrase);
          } else if (typeof cb === 'object' && cb.regexp instanceof RegExp) {
            // register the command
            registerCommand(new RegExp(cb.regexp.source, 'i'), cb.callback, phrase);
          }
        }
      }
    },
    executeCommand: function(commandText) {
      for (var j = 0, l = commandsList.length; j < l; j++) {
        var result = commandsList[j].command.exec(commandText);
        if (result) {
          var parameters = result.slice(1);
          // execute the matched command
          commandsList[j].callback.apply(this, parameters);
          return true;
        }
      }
    }
  };
}).call(this)
运行应用程序时,执行
addCommands
命令,并根据用户的输入命令执行
executeCommand
命令

上面的内容对我来说很好,但我正转向C#,而且对C#还很陌生,所以我想寻求帮助,至少是一些C#功能和工具的指导,可以帮助我编写类似于上面的内容

更新
关于我尝试做的更多细节,实际上我有一个表单,用户使用HTL5语音API通过语音输入他的命令,API将此语音转换为文本,然后我将此文本提交到我的应用程序,我的应用程序从查看此文本开始,尝试使用ReqEx查找所需的命令,然后执行与此输入命令映射的已编程函数/回调。

我使用System.Collections.Generic在
中的
字典
找到了解决方案
并使用
RegEx
at
使用System.Text.regular表达式
以及名为
FirstOrDefault
的函数的需要,可在
使用System.Linq

我使用了
Action
而不是function
Func
,因为在我的例子中回调是不返回任何内容的,即它们是
void
函数,并且因为没有提供输入参数,所以我使用了
Action
-委托,而没有使用
Action

工作守则是:

using System;
using System.Collections.Generic;  // for Dictionary
using System.Linq;  // for FirstOrDefault
using System.Text.RegularExpressions;  // for RegEx

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var input = "What's the meaning of Stretch Hood";

                    var functions = new Dictionary<Regex, Action>
                    {
                        {new Regex("/^(What is|What's|Could you please tell me|Could you please give me) the meaning of (TF|FFS|SF|SHF|FF|Tube Film|Shrink Film|Stretch Hood|Stretch Hood Film|Flat Film)$/"),
                             itemsIdentification},
                        {new Regex("/^(What is|What's|Could you please tell me|Could you please give me) the (stock|inventory) of ML$/"),
                             mlSOH},
                        {new Regex("/^(What is|What's) (our|the) (stock|inventory|SoH) of (TF|FFS|SF|SHF|FF|Tube Film|Shrink Film|Stretch Hood|Stretch Hood Film|Flat Film)$/"),
                             SoH},
                        {new Regex("/^(What is|What's|Calculate|How much is) ([\w.]+) (\+|and|plus|\-|less|minus|\*|\x|by|multiplied by|\/|over|divided by) ([\w.]+)$/"),
                             math},
                    };
            functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value?.Invoke();    // This will execute the first Action found wherever the input matching the RegEx, the ?. means if not null ([Null-conditional Operators][1])
// or 
            Action action;
            action = functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value;
            if (action != null)
            {
                action.Invoke();
            }
            else
            {
                // No function with that name
            }
        }

        public static void itemsIdentification()
        {
        Console.WriteLine("Fn 1");
        }

        public static void mlSOH()
        {
        Console.WriteLine("Fn 2");
        }
        public static void SoH()
        {

        }
        public static void math()
        {

        }
    }
}
使用系统;
使用System.Collections.Generic;//词典
使用System.Linq;//第一道防线
使用System.Text.RegularExpressions;//正则表达式
命名空间控制台应用程序
{
公共课程
{
公共静态void Main(字符串[]args)
{
控制台。WriteLine(“你好,世界!”);
var input=“拉伸罩的含义是什么”;
var函数=新字典
{
{新规则(“/^(什么是|什么是|你能告诉我|你能告诉我|你能告诉我吗|你能告诉我吗)的含义(TF | FFS | SF | SHF | FF |管膜|收缩膜|拉伸罩|拉伸罩膜|平板膜)$/”,
itemsIdentification},
{new Regex(“/^(What is | What's |能否请您告诉我|能否请您给我)ML$/”的(库存|库存),
mlSOH},
{新规则(“/^(What is | What's)(我们的|库存| SoH)(TF | FFS | SF | SHF | FF |管膜|收缩膜|拉伸罩|拉伸罩膜|平板膜)$/”,
SoH},
{new Regex(“/^(What is | What is | Calculate | What is | What is | Calculate | | | How is | | | | | | | | |+)(\+|和|加|-| | | | | | | | | |,
数学},
};
functions.FirstOrDefault(f=>f.Key.IsMatch(input)).Value?.Invoke();//这将执行找到的第一个操作,只要输入与正则表达式匹配,.means如果不是null([null条件运算符][1])
//或
行动;
action=functions.FirstOrDefault(f=>f.Key.IsMatch(input)).Value;
如果(操作!=null)
{
action.Invoke();
}
其他的
{
//没有使用该名称的函数
}
}
公共静态void itemsIdentification()
{
控制台写入线(“Fn 1”);
}
公共静态无效mlSOH()
{
控制台写入线(“Fn 2”);
}
公共静态无效SoH()
{
}
公共静态无效数学()
{
}
}
}

C#具有相同的流控制
if-else
for loop
。C#have
functions
classes
、collections
List
Dictionary
作为带函数的Dictionary和C#have
Regex
也一样-如果您展示您尝试过什么,我们可以帮助解决一些特定的问题,那会更好。@Fabio我更新了我的问题,解释了更多需要的内容,我想你指的
字典
可能接近我所需要的,我会搜索并检查。感谢“使用正则表达式将JavaScript代码转换为C#”-此标题听起来像是您想使用正则表达式将JS转换为C#。@nnnn I修改了标题,现在更符合逻辑了吗?@Fabio您能在这里提供帮助吗
using System;
using System.Collections.Generic;  // for Dictionary
using System.Linq;  // for FirstOrDefault
using System.Text.RegularExpressions;  // for RegEx

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var input = "What's the meaning of Stretch Hood";

                    var functions = new Dictionary<Regex, Action>
                    {
                        {new Regex("/^(What is|What's|Could you please tell me|Could you please give me) the meaning of (TF|FFS|SF|SHF|FF|Tube Film|Shrink Film|Stretch Hood|Stretch Hood Film|Flat Film)$/"),
                             itemsIdentification},
                        {new Regex("/^(What is|What's|Could you please tell me|Could you please give me) the (stock|inventory) of ML$/"),
                             mlSOH},
                        {new Regex("/^(What is|What's) (our|the) (stock|inventory|SoH) of (TF|FFS|SF|SHF|FF|Tube Film|Shrink Film|Stretch Hood|Stretch Hood Film|Flat Film)$/"),
                             SoH},
                        {new Regex("/^(What is|What's|Calculate|How much is) ([\w.]+) (\+|and|plus|\-|less|minus|\*|\x|by|multiplied by|\/|over|divided by) ([\w.]+)$/"),
                             math},
                    };
            functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value?.Invoke();    // This will execute the first Action found wherever the input matching the RegEx, the ?. means if not null ([Null-conditional Operators][1])
// or 
            Action action;
            action = functions.FirstOrDefault(f => f.Key.IsMatch(input)).Value;
            if (action != null)
            {
                action.Invoke();
            }
            else
            {
                // No function with that name
            }
        }

        public static void itemsIdentification()
        {
        Console.WriteLine("Fn 1");
        }

        public static void mlSOH()
        {
        Console.WriteLine("Fn 2");
        }
        public static void SoH()
        {

        }
        public static void math()
        {

        }
    }
}