Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regex to allow only certain Characters

Last post 05-15-2008, 4:23 AM by colinejarvis. 7 replies.
Sort Posts: Previous Next
  •  05-14-2008, 8:00 AM 42236

    Regex to allow only certain Characters

    Hi Iam trying to build a regex to allow 0-9 and A-Z (Capitalised) with a string length of 18 characters and have the following code:

    ^([A-Z0-9]){18}

    However I need to allow "spaces" "ampersands" "hyphens" "full Stops" and "forward slash" 

    Please could someone advise on how these characters can also be added

    Many thanks in anticipation

    Rgds

    Colin 

     

  •  05-14-2008, 8:24 AM 42237 in reply to 42236

    Re: Regex to allow only certain Characters

    I am also not a expert in RegEx, but I guess I know the solution for your issue.

    Try with this, simple mistake :-), 

    ^([A-Z]|[0-9]){18}

    it should check for characters and digits, if you want to include more special characters to allow, attach to the same string with the pipeline "|" mark. Your expr.. also may work, but you can keep it clean by having seperators like above.

    You may check for special chars syntax to represent in expression in the following,

     http----//www----devguru----com/Technologies/ecmaScript/quickref/regexp_special_characters.html

    If you want to allow a space then it's "\s", (you can find more in the above), the expression will be like

    ^([A-Z]|[0-9]|[\s]){18}

     

     hope this would help you understand about regex...

     

    Thanks, 

    Chinthana

     

  •  05-14-2008, 8:42 AM 42239 in reply to 42237

    Re: Regex to allow only certain Characters

    Hi Chinthana,

    Thanks for your reply.

    I tried

    ^([A-Z]|[0-9]|[ ]|Dog|[-]|[.]|[/]){18}

    but it did not allow either numeric or special charcters

    Any Suggestions?

    Rgds

    Colin

     

  •  05-14-2008, 8:49 AM 42240 in reply to 42239

    Re: Regex to allow only certain Characters

    ^([A-Z]|[0-9]|[\s]){18}

     Well... you can validate the expression in http----//www---fileformat--info/tool/regex.htm

    It should work. Note that your character limit is set to 18.

     

    It's all I can do for now.

     

    Thanks,

    Chinthana 

     

  •  05-14-2008, 9:46 AM 42241 in reply to 42240

    Re: Regex to allow only certain Characters

    I have adjusted the regex to

    ^([A-Z]|[0-9]|[\s]|[/]){1,18}

    and this will alow A-Z 1-9 space and / as the first character of the string subsequent characters can be lowercase or other special characters.

    I need to allow only capitals throughout the string and only those special characters that I have specified

    Rgds

    Colin

     

  •  05-14-2008, 10:11 AM 42243 in reply to 42236

    Re: Regex to allow only certain Characters

    colinejarvis:

    Hi Iam trying to build a regex to allow 0-9 and A-Z (Capitalised) with a string length of 18 characters

    However I need to allow "spaces" "ampersands" "hyphens" "full Stops" and "forward slash" 

    ^[A-Z0-9 &./-]{18}$




    looking for a new regex book?
    Regular Expressions Cookbook
  •  05-14-2008, 11:19 AM 42251 in reply to 42237

    Re: Regex to allow only certain Characters

    chinthana:

    I am also not a expert in RegEx, but I guess I know the solution for your issue.

    Try with this, simple mistake :-), 

    ^([A-Z]|[0-9]){18}

    it should check for characters and digits, if you want to include more special characters to allow, attach to the same string with the pipeline "|" mark. Your expr.. also may work, but you can keep it clean by having seperators like above.

    You may check for special chars syntax to represent in expression in the following,

     http----//www----devguru----com/Technologies/ecmaScript/quickref/regexp_special_characters.html

    If you want to allow a space then it's "\s", (you can find more in the above), the expression will be like

    ^([A-Z]|[0-9]|[\s]){18}

     

     hope this would help you understand about regex...

     

    Thanks, 

    Chinthana

     

    You do not appear to have a clear grasp of the Character Class.

    Using alternation isn't need in this case. It not wrong just unnecessarily verbose  As ddrurik demonstrated you only need to add the desired characters to the character class. 

    http://regexadvice.com/blogs/mash/archive/2008/01/31/A-touch-of-Character-Class.aspx

    Also \s doesn't just match a space. So you are overmatching the requirements 


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-15-2008, 4:23 AM 42291 in reply to 42243

    Re: Regex to allow only certain Characters

    Many thanks for your help.

    Looking at the answer increases my understanding on the fly and you help is much appreciated

    Kind regards

     Colin

View as RSS news feed in XML