Reg. exp. question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy

    #1

    Reg. exp. question

    Hi all,

    I need to check a string for valid characters. The regular expression that I
    am working is long and I try to make it short. There are many 'or' condition
    in the expression, so if I can find answer to the following question then I
    can make the reg exp shorter and nicer.
    Now example: There is a word that could be 2 or 3 characters. First
    characters should be "a" and the last character should be "d". The middle
    character is either "n" or nothing. My regular expression looks like:
    [a][n][d]|[a][d]
    Is there a way to make the "d" optional as character d or nothing and not
    use (|).
    Thanks in advance,

    Roy
  • Jacques Leclerc

    #2
    RE: Reg. exp. question

    Don't know if this will make it any shorter.
    an{0,1}d

    {0,1} specifies that the expression will accept 0 or 1 occurances of the
    preceding character or expression.

    Hope this helps

    Jacques

    "Roy" wrote:
    [color=blue]
    > Hi all,
    >
    > I need to check a string for valid characters. The regular expression that I
    > am working is long and I try to make it short. There are many 'or' condition
    > in the expression, so if I can find answer to the following question then I
    > can make the reg exp shorter and nicer.
    > Now example: There is a word that could be 2 or 3 characters. First
    > characters should be "a" and the last character should be "d". The middle
    > character is either "n" or nothing. My regular expression looks like:
    > [a][n][d]|[a][d]
    > Is there a way to make the "d" optional as character d or nothing and not
    > use (|).
    > Thanks in advance,
    >
    > Roy[/color]

    Comment

    Working...