4

我正在C#使用中构建一个聊天机器人AIML files,目前我有这段代码要处理:

<aiml>
    <category>
        <pattern>a * is a *</pattern>
        <template>when a <star index="1"/> is not a <star index="2"/>?</template>
    </category>
</aiml>

我想做类似的事情:

if (user_string == pattern_string) return template_string;

但我不知道如何告诉计算机star字符可以是任何东西,特别是可以不止一个字!我想用正则表达式来做,但我没有足够的经验。有人可以帮助我吗?:)

4

2 回答 2

2

使用正则表达式

static bool TryParse(string pattern, string text, out string[] wildcardValues)
{
    // ^ and $ means that whole string must be matched
    // Regex.Escape (http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape(v=vs.110).aspx)
    // (.+) means capture at least one character and place it in match.Groups
    var regexPattern = string.Format("^{0}$", Regex.Escape(pattern).Replace(@"\*", "(.+)"));

    var match = Regex.Match(text, regexPattern, RegexOptions.Singleline);
    if (!match.Success)
    {
        wildcardValues = null;
        return false;
    }

    //skip the first one since it is the whole text
    wildcardValues = match.Groups.Cast<Group>().Skip(1).Select(i => i.Value).ToArray();
    return true;
}

示例使用

string[] wildcardValues;
if(TryParse("Hello *. * * to *", "Hello World. Happy holidays to all", out wildcardValues))
{
    //it's a match
    //wildcardValues contains the values of the wildcard which is
    //['World','Happy','holidays','all'] in this sample
}

顺便说一句,你真的不需要正则表达式,这有点矫枉过正。只需通过使用 string.Split 将模式拆分为标记然后使用 string.IndexOf 查找每个标记来实现您自己的算法。尽管使用 Regex 确实会缩短代码

于 2013-12-23T18:37:27.227 回答
0

你认为这对你有用吗?

Match match = Regex.Match(pattern_string, @"<pattern>a [^<]+ is a [^<]+</pattern>");
if (match.Success)
{
    // do something...
}

这里[^<]+代表一个或多个字符是/不是<

如果您认为您的 * 中可能有 < 字符,那么您可以简单地使用.+而不是[^<]+
但这将是有风险的,因为.+表示任何字符有一次或多次。

于 2013-12-23T16:49:51.200 回答