#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Regex for Caps on the first character of each work in C#


    I used the CLR capability in SQL Server 2012 to create a function that compares a field (First Expression) to the regex which is my second expression of the function

    I'm stuck on this regex.

    I want to verify that the first Letter of each word from the beginning of the string until the end is capitalized.

    I am struggling --0

    I Am Struggling -- 0

    I have this so far.

    [\p{Lu}\p{Lt}]
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Two ways of doing it: either you check to see if there are any uncapitalized words, or you make sure that every word is capitalized. The former is much easier as you only have to look for whitespace + a lowercase letter,
    Code:
    \p{Zs}\p{Ll}
    Otherwise you've got to scan through the entire string,
    Code:
    ^(\p{Zs}*[^\p{Ll}\p{Zs}]\P{Zs}*)*\p{Zs}*$
    - \p{Zs} for leading whitespace
    - Neither \p{Ll} nor \p{Zs} as the initial "letter"
    - \P{Zs}* for the remainder of the "word"
    - \p{Zs} for trailing whitespace
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Thankful for solution


    Thanks so much I especially appreciate the explanations of the regex.

IMN logo majestic logo threadwatch logo seochat tools logo