How to find a line with missing fragments?

I am creating chatbot in C#using AIML files, at the moment I am processing this code:

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

I would like to do something like:

if (user_string == pattern_string) return template_string;

but I don’t know how to tell the computer that a character starcan be anything, and, most importantly, it can be a few words! I thought of doing this with regular expressions, but I have not enough experience. Can someone help me? :)

+4
source share
2 answers

Using Regex

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;
}

Using example

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

, Regex, . , , string.Split, , string.IndexOf. Regex

+2

, ?

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

[^ <] + , <

, < *, . + [^ <] +
, . + , .

0

All Articles