Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: compare two textboxes (string) - need help Pin
harold aptroot11-Sep-16 1:41
harold aptroot11-Sep-16 1:41 
AnswerRe: compare two textboxes (string) - need help Pin
#realJSOP11-Sep-16 3:30
mve#realJSOP11-Sep-16 3:30 
AnswerRe: compare two textboxes (string) - need help Pin
V.11-Sep-16 21:10
professionalV.11-Sep-16 21:10 
QuestionHow is check the date of a valid dateEdit ? Pin
Member 245846710-Sep-16 23:42
Member 245846710-Sep-16 23:42 
AnswerRe: How is check the date of a valid dateEdit ? Pin
OriginalGriff11-Sep-16 0:03
mveOriginalGriff11-Sep-16 0:03 
AnswerRe: How is check the date of a valid dateEdit ? Pin
#realJSOP11-Sep-16 3:21
mve#realJSOP11-Sep-16 3:21 
QuestionGroup Sequance patterns with Linq Pin
Member 121069269-Sep-16 1:27
Member 121069269-Sep-16 1:27 
AnswerRe: Group Sequance patterns with Linq Pin
Richard Deeming9-Sep-16 2:02
mveRichard Deeming9-Sep-16 2:02 
I don't think the built-in LINQ methods will help you here. But it's fairly simple to implement:
C#
public static class Patterns
{
    public static IEnumerable<string> GroupPatterns(this string pattern)
    {
        char prev = '\0';
        char[] item = new char[pattern.Length];
        int itemIndex = 0;
        
        foreach (char c in pattern)
        {
            if (c != prev)
            {
                if (itemIndex != 0)
                {
                    yield return new string(item, 0, itemIndex);
                    Array.Clear(item, 0, itemIndex);
                    itemIndex = 0;
                }
                
                prev = c;
            }
            
            item[itemIndex] = c;
            itemIndex++;
        }
        
        if (itemIndex != 0)
        {
            yield return new string(item, 0, itemIndex);
        }
    }
}

Given the input "DDDD-AAAA-DA-AD", that will produce:
{
    "DDDD",
    "-",
    "AAAA",
    "-",
    "D",
    "A",
    "-",
    "A",
    "D"
}


As to finding the matches, it might be simpler to convert the pattern to a Regular Expression[^]:
C#
public static class Patterns
{
    ...
    
    public static Regex PatternToRegularExpression(this string pattern)
    {
        var parts = new List<string>();
        foreach (string part in pattern.GroupPattern())
        {
            switch (part[0])
            {
                case 'D':
                case 'd':
                {
                    parts.Add("\\d{" + part.Length + "}");
                    break;
                }
                case 'A':
                case 'a':
                {
                    parts.Add("[A-Z]{" + part.Length + "}");
                    break;
                }
                default:
                {
                    parts.Add(Regex.Escape(part));
                    break;
                }
            }
        }
        
        string regexPattern = string.Concat(parts);
        return new Regex(regexPattern, RegexOptions.IgnoreCase);
    }
}

For the same input, this will generate the regular expression:
\d{4}-[a-z]{4}-\d{1}[a-z]{1}-[a-z]{1}\d{1}

Regexper[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Group Sequance patterns with Linq Pin
Member 121069269-Sep-16 2:10
Member 121069269-Sep-16 2:10 
AnswerRe: Group Sequance patterns with Linq Pin
#realJSOP9-Sep-16 2:14
mve#realJSOP9-Sep-16 2:14 
GeneralRe: Group Sequance patterns with Linq Pin
Member 121069269-Sep-16 2:19
Member 121069269-Sep-16 2:19 
GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP9-Sep-16 2:25
mve#realJSOP9-Sep-16 2:25 
GeneralRe: Group Sequance patterns with Linq Pin
Member 121069269-Sep-16 2:28
Member 121069269-Sep-16 2:28 
SuggestionRe: Group Sequance patterns with Linq Pin
Richard Deeming9-Sep-16 4:15
mveRichard Deeming9-Sep-16 4:15 
GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP9-Sep-16 4:18
mve#realJSOP9-Sep-16 4:18 
GeneralRe: Group Sequance patterns with Linq Pin
Richard Deeming9-Sep-16 4:19
mveRichard Deeming9-Sep-16 4:19 
GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP9-Sep-16 4:25
mve#realJSOP9-Sep-16 4:25 
AnswerRe: Group Sequance patterns with Linq Pin
Member 121069269-Sep-16 2:26
Member 121069269-Sep-16 2:26 
AnswerRe: Group Sequance patterns with Linq Pin
Gerry Schmitz9-Sep-16 13:30
mveGerry Schmitz9-Sep-16 13:30 
AnswerRe: Group Sequance patterns with Linq Pin
BillWoodruff10-Sep-16 2:34
professionalBillWoodruff10-Sep-16 2:34 
GeneralRe: Group Sequance patterns with Linq Pin
#realJSOP11-Sep-16 3:18
mve#realJSOP11-Sep-16 3:18 
GeneralRe: Group Sequance patterns with Linq Pin
BillWoodruff11-Sep-16 19:21
professionalBillWoodruff11-Sep-16 19:21 
Questionunable to bind json data in label Pin
Member 127294328-Sep-16 22:36
Member 127294328-Sep-16 22:36 
QuestionRe: unable to bind json data in label Pin
Richard MacCutchan8-Sep-16 22:40
mveRichard MacCutchan8-Sep-16 22:40 
AnswerRe: unable to bind json data in label Pin
Member 127294328-Sep-16 22:46
Member 127294328-Sep-16 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.