Douglas Moody
Douglas Moody

Reputation: 47

How to filter a parameter that contains numbers? C# .NET

If the QnA result replies with "111card.json" I need the 111 to be identified await and forward to a separate class object then return.

[LuisIntent("it.support")]
public async Task ITSupportIntent(IDialogContext context, LuisResult result)       
{
        var qnaResult = itKB.GetAnswer(result.Query);

        if (qnaResult.ToLower().Contains("111") || 
            qnaResult.ToLower().Contains("222") || 
            qnaResult.ToLower().Contains("333")) 
        {

Currently have the above but not sure if this will work. It should be able to identify the 111 which is part of 111card.json.

Bonus points for showing multiple means of filtering (text, numbers, symbols) etc.

Upvotes: 1

Views: 75

Answers (2)

itsme86
itsme86

Reputation: 19496

This is exactly the kind of thing Regex is great at:

Regex pattern = new Regex(@"(?<num>\d{3})card\.json");
Match match = pattern.Match(qnaResult.ToLower());
if (match.Success)
{
    string num = pattern.Groups["num"].Value;
    // Do something with num
}
else
{
    // No match
}

Upvotes: 2

Lews Therin
Lews Therin

Reputation: 3777

I'd recommend looking in to Regular Expressions.

using System.Text.RegularExpressions;

var patternToMatch = "[0-9]{3}card\.json";
var regex = new Regex(patternToMatch, RegexOptions.IgnoreCase);

if (regex.IsMatch(qnaResult))
{
    // Do something...
}

Upvotes: 2

Related Questions