locked
Event Receivers / Regular Expressions RRS feed

  • Question

  • Hi,

    I have an Event Receiver which aims to ensure that the first letter of a word is a capital. I would like to use regular expressions, but I am new to it. 

    Here's the code. Basically, what I would like to happen is that the Event Receiver comes up if the word does not have a capital letter at its start in when entered in the DayNew column.

    What regular expression code should I use? I have tried the below, but is doesn't work.

                  

    if (!string.IsNullOrEmpty(sDayNew))
               {
                   if (!System.Text.RegularExpressions.Regex.IsMatch(sDayNew, @"^[a-z]$"))

                   {
                       properties.Cancel = true;
                   }
               }

    THANKS!

    Zuke


    zuke collins

    Tuesday, September 25, 2012 1:39 PM

Answers

  • Hi

    Why don't you try using char.IsUpper(sDayNew,0) instead of using a Regular Expression.

    Regards,

    Pedro


    Pedro Aparcana

    • Marked as answer by zukecollins Wednesday, September 26, 2012 10:46 AM
    Tuesday, September 25, 2012 8:18 PM

All replies

  • Hi

    Why don't you try using char.IsUpper(sDayNew,0) instead of using a Regular Expression.

    Regards,

    Pedro


    Pedro Aparcana

    • Marked as answer by zukecollins Wednesday, September 26, 2012 10:46 AM
    Tuesday, September 25, 2012 8:18 PM
  • Hi Pedro,

    That's great - I was using Regular Expressions as I was working off an exercise from a book. 

    However, what would you do if, for example you wanted to check that the start of each word was upper case and you had three words?

    Many thanks

    Zuke


    zuke collins

    Wednesday, September 26, 2012 10:48 AM
  • I don't know much about regular expressions but I would do this :

    string splitWords = sDayNew.Split(' ');
    bool isOk = true;
    for(int i = 0; i < splitWords.Length; i++)
      if(!char.IsUpper(splitWords[i][0])
        isOk = false;
    
    if(!isOk) 
    {
      // here you put the code if every word doesn't start with a capital letter.
    }

    This is just one solution, maybe you can do it by using regex in some other way


    Pedro Aparcana

    Wednesday, September 26, 2012 2:58 PM