0
var managementCount = from tbdocheader in context.tblDocumentHeaders
                      join tbDocRevision in context.tblDocumentRevisions
                      on tbdocheader.DocumentHeaderID equals tbDocRevision.DocumentHeaderID
                      select new { tbdocheader, tbDocRevision };    

var query =(from obj in managementCount.AsEnumerable()
                             where Regex.IsMatch(obj.tbDocRevision.Revision, @"[A-Za-z]%")
                             select obj).Count(); 

I'm trying to get the records count where Revision starts with an alphabet."managementCount" query returns records with "Revision=A", but my query does not returns any matching records.

is something wrong with my regular expression?

ErikE
  • 48,881
  • 23
  • 151
  • 196
chamara
  • 12,649
  • 32
  • 134
  • 210

3 Answers3

2

Try the pattern "^[A-Za-z]*$

Here

 ^ indicates start of an expression,

 $ indicates end of an expression,

 [A-Za-z] will allow any alphabet character and

 [A-Za-z]* will allow any length of alphabet characters.

In C# code you will write :

@"^[A-Za-z]*$

Here, the @ symbol means to read that string literally, and don't interpret control characters otherwise

I hope this will help you..!

Naresh Ravlani
  • 1,600
  • 13
  • 28
  • hi! do I have to change the regular expression when executing a sql query in sql server? because it seems like expression has to be [A-Za-z]% for sql server and yes,above expression works fine only with LINQ query – chamara Mar 20 '14 at 07:00
  • in SQL % serves same purpose as * in regex. It means [A-Za-z]% will allow any length of alphabet characters. – Naresh Ravlani Mar 20 '14 at 07:19
  • 1
    "Here again @ indicates start of starting of an expression" is wrong. The @ symbol indicates a verbatim string, not an expression. See [this question](http://stackoverflow.com/questions/3311988/what-is-the-difference-between-a-regular-string-and-a-verbatim-string) – cremor Mar 20 '14 at 07:29
  • @cremor Thanks man..! My apologies. I have edited answer with correction. – Naresh Ravlani Mar 20 '14 at 07:32
  • % and * are NOT the sane. % modifies the last matching element and does not work ny itself. – ErikE Mar 20 '14 at 15:13
1

I think you're looking for the pattern "^[a-z]" with extra parameter RegexOptions.IgnoreCase.

It looks to me like you're used to SQL LIKE syntax. Regular expressions are different--they use different wildcard characters, have many more matching abilities, by default match multiple times in a string, and are also a lot harder to get right. SQL LIKE patterns are always implicitly anchored at the ends, and Regexes are not.

So the pattern above means, match starting at the beginning of the string ^, and then be followed by a letter. There is no need to add a wildcard character because Regexes are not anchored by default.

I encourage you to go do some reading and study. Try regular-expressions.info.

ErikE
  • 48,881
  • 23
  • 151
  • 196
  • hi! when I used ^[A-Za-z] it works fine, however I tried the same reg expression in sql server and it did not work. For sql server I had to change the regular expression to [A-Za-z]%.what could be the reason ? – chamara Mar 20 '14 at 06:50
  • SQL Server does not implement true regular expressions, just some basic pattern matching. The syntax is different. – ErikE Mar 20 '14 at 15:11
1

Try the pattern "^[A-Za-z]"...

var query =(from obj in managementCount.AsEnumerable()
                         where Regex.IsMatch(obj.tbDocRevision.Revision, @"^[A-Za-z]")
                         select obj).Count(); 
Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • hi! when I used ^[A-Za-z] it works fine, however I tried the same reg expression in sql server and it did not work. For sql server I had to change the regular expression to [A-Za-z]%.what could be the reason ? – chamara Mar 20 '14 at 06:51
  • 1
    Regex.IsMatch() executes in .NET, not TSQL, so it uses standard regex syntax. – Anthony Chu Mar 20 '14 at 06:58