Using regular expression validator to limit string to only four characters

Last post 06-23-2008 9:18 PM by jamesqua. 4 replies.

Sort Posts:

  • Using regular expression validator to limit string to only four characters

    05-14-2008, 12:21 PM
    • Loading...
    • mhariri
    • Joined on 11-15-2004, 8:35 PM
    • Posts 458

    Hi,

    I am trying to use the regular expressions to check if the input value has any characters other than T,C,G and A. I am using the following expression

    [a-z-[bdefhijklmnopqrsuvwxyz]]*|[A-Z-[BDEFHIJKLMNOPQRSUVWXYZ]]*

    But it only allows one character "T" to pass others fail sor t will check ok but  TCGA or TC or TCGAGACT fails. Any ideas about how to construct the expression so the string is passed if only contains T,C,G and A. It should fail if it conatins any other characters.

     

    Thanks

  • Re: Using regular expression validator to limit string to only four characters

    05-14-2008, 7:15 PM
    Answer
    • Loading...
    • SGWellens
    • Joined on 01-02-2007, 4:27 PM
    • MN, USA
    • Posts 2,575
    • Moderator
      TrustedFriends-MVPs

    Try this:     [T|C|G|A]*

     

    FYI:

    Ultrapico This site has a great free tool for building Regular Expressions (Expresso).
    Steve Wellens
  • Re: Using regular expression validator to limit string to only four characters

    05-15-2008, 1:42 PM
    • Loading...
    • mhariri
    • Joined on 11-15-2004, 8:35 PM
    • Posts 458

    Thanks a million.

  • Re: Using regular expression validator to limit string to only four characters

    06-23-2008, 2:26 PM
    • Loading...
    • mhariri
    • Joined on 11-15-2004, 8:35 PM
    • Posts 458

    Do you have an example on how to return only [A|T|C|G] from a long string of characters? For example if yo have

     1. aagtcctgcttc 2.agtctccttc

    It would return

    aagtcctgcttcagtctccttc

    Hence removing all characters other than ATCG? 

     

  • Re: Using regular expression validator to limit string to only four characters

    06-23-2008, 9:18 PM
    • Loading...
    • jamesqua
    • Joined on 08-03-2004, 2:55 PM
    • Columbus, OH
    • Posts 1,078

     

    string Test = "1.atgc2.gtcg98#sl";
    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^atcgATCG]{1,}");
    string result = regex.Replace(Test, "");
    Console.WriteLine(result);
     
Page 1 of 1 (5 items)