locked
Need Regular Expression which allows alphanumeric also & - _ special characters with only one space between

    Question

  • Need Regular expression

    which allows alphanumeric along with  special characters like &  - _  with only one space between the letters.Special characters should not be at start and at the end of the input string.There should not be 2 successive special characters.

    following scenario should accept:

    asdd ads add

    Asd123&ds

    AS_df_23 ff-f

     

    Following Scenario should not accept:

    _sa&ff

    Asd   fff

    &-_

    _d

    only special characters

    asdf_

     

    Please suggest solution for above scenario.

     

     

     

     

     

    Wednesday, April 13, 2011 12:49 PM

Answers

  • Hi,

    is it not quite the same as in your last question?

    ^(?:(?>[a-zA-Z0-9]+)(?:[-&_ ](?![-&_ ]|$))?)+$

    I use [a-zA-Z0-9] for "normal" characters and [-&_ ] for special characters and whitespace. If a "special character" in your context is "any character but [a-zA-Z0-9], you can also use [^a-zA-Z0-9\r\n] (add \r\n to avoid line breaks to match)

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Marked as answer by Ninds001 Thursday, April 14, 2011 9:24 AM
    Wednesday, April 13, 2011 1:40 PM

All replies

  • Hi,

    is it not quite the same as in your last question?

    ^(?:(?>[a-zA-Z0-9]+)(?:[-&_ ](?![-&_ ]|$))?)+$

    I use [a-zA-Z0-9] for "normal" characters and [-&_ ] for special characters and whitespace. If a "special character" in your context is "any character but [a-zA-Z0-9], you can also use [^a-zA-Z0-9\r\n] (add \r\n to avoid line breaks to match)

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Marked as answer by Ninds001 Thursday, April 14, 2011 9:24 AM
    Wednesday, April 13, 2011 1:40 PM
  • On Wed, 13 Apr 2011 12:49:56 +0000, Ninds001 wrote:
     
    >
    >
    >Need Regular expression
    >
    >which allows alphanumeric along with  special characters like &  - _  with only one space between the letters.Special characters should not be at start and at the end of the input string.There should not be 2 successive special characters.
    >
    >following scenario should accept:
    >
    >asdd ads add
    >
    >Asd123&ds
    >
    >AS_df_23 ff-f
    >
    >
    >Following Scenario should not accept:
    >
    >_sa&ff
    >
    >Asd   fff
    >
    >&-_
    >
    >_d
    >
    >only special characters
    >
    >asdf_
    >
    >
    >Please suggest solution for above scenario.
    >
     
    I think this will work:
     
    ^[A-Za-z0-9]?(?:[A-Za-z0-9]+[-&_\s])+[A-Za-z0-9]+$
     
     

    Ron
    Wednesday, April 13, 2011 2:14 PM
  • Thank u so much

     

     

    Thursday, April 14, 2011 9:25 AM
  • Hi,

      I need a regular expression for allowing alpha numeric and period but there cannot be a period in the 1st position and also not the last 2 positions. I have tried several ways, nothing seems to work. Any help would be greatly appreciated.

    Thursday, November 10, 2011 7:59 PM
  • ^[A-Z\d][A-Z\d.]*[A-Z\d]{2}

    with ignore case option set matches what you describe.  However, if this is not what you want, please read the sticky entitled "How to ask a Regular Expression Question" and re-post your question in accordance with those guidelines.

    Also, it is usually best to start a new thread, rather than add on to a long-dead previously answered thread.


    Ron
    Thursday, November 10, 2011 8:31 PM