hyphen in regular expression (cSharp)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jayanthigk2004@yahoo.com

    hyphen in regular expression (cSharp)

    Input
    a-

    Patterns

    \w*-*
    \w*(-*)
    \w*(-)*
    \w*[-]*

    No of the patterns results in a match. Tried escaps(\) before hyphen
    still no match.

    My requirement is to make sure a string contains only digits, alphabets
    and hypen (each 0 or more times).

  • Kevin Spencer

    #2
    Re: hyphen in regular expression (cSharp)

    Well, you're not being very specific about your requirements, but the
    following would work with them as you have stated them:

    [\w-]*

    Translated: Match any combination of 0 or more of digits, alphabetical
    characters, or hyphens.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Computer Control Freak
    Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


    A man, a plan, a canal, a palindrome that has.. oh, never mind.

    <jayanthigk2004 @yahoo.comwrote in message
    news:1160345578 .187544.302930@ k70g2000cwa.goo glegroups.com.. .
    Input
    a-
    >
    Patterns
    >
    \w*-*
    \w*(-*)
    \w*(-)*
    \w*[-]*
    >
    No of the patterns results in a match. Tried escaps(\) before hyphen
    still no match.
    >
    My requirement is to make sure a string contains only digits, alphabets
    and hypen (each 0 or more times).
    >

    Comment

    • Truong Hong Thi

      #3
      Re: hyphen in regular expression (cSharp)


      I would think all of those patterns result in matches. Could it be some
      mistake in your code?

      jayanthigk2004@ yahoo.com wrote:
      Input
      a-
      >
      Patterns
      >
      \w*-*
      \w*(-*)
      \w*(-)*
      \w*[-]*
      >
      No of the patterns results in a match. Tried escaps(\) before hyphen
      still no match.
      >
      My requirement is to make sure a string contains only digits, alphabets
      and hypen (each 0 or more times).

      Comment

      • Kevin Spencer

        #4
        Re: hyphen in regular expression (cSharp)

        The patterns would certainly match "a-" but not the requirements he posted.
        It is a matter of sequence. The regular expression:

        \w*-*

        literally means "a match is zero or more word characters *followed by* zero
        or more hyphen characters. So, the string "-a" would result in 2 matches,
        one for the "-" and one for the "a", while the regular expression:

        [\w-]*

        literally means "a match is any combination 0 or more of word characters
        and/or hypens, so that the string "-a" would result in a single match
        containing both characters.

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        Chicken Salad Shooter
        Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


        A man, a plan, a canal, a palindrome that has.. oh, never mind.

        "Truong Hong Thi" <thi1981@gmail. comwrote in message
        news:1160370247 .045671.246630@ i42g2000cwa.goo glegroups.com.. .
        >
        I would think all of those patterns result in matches. Could it be some
        mistake in your code?
        >
        jayanthigk2004@ yahoo.com wrote:
        >Input
        >a-
        >>
        >Patterns
        >>
        >\w*-*
        >\w*(-*)
        >\w*(-)*
        >\w*[-]*
        >>
        >No of the patterns results in a match. Tried escaps(\) before hyphen
        >still no match.
        >>
        >My requirement is to make sure a string contains only digits, alphabets
        >and hypen (each 0 or more times).
        >

        Comment

        • jayanthigk2004@yahoo.com

          #5
          Re: hyphen in regular expression (cSharp)

          My requirement is for validation and it should allow only digits,
          alphabets and hyphens in the text the user enters.

          Thanks

          Kevin Spencer wrote:
          The patterns would certainly match "a-" but not the requirements he posted.
          It is a matter of sequence. The regular expression:
          >
          \w*-*
          >
          literally means "a match is zero or more word characters *followed by* zero
          or more hyphen characters. So, the string "-a" would result in 2 matches,
          one for the "-" and one for the "a", while the regular expression:
          >
          [\w-]*
          >
          literally means "a match is any combination 0 or more of word characters
          and/or hypens, so that the string "-a" would result in a single match
          containing both characters.
          >
          --
          HTH,
          >
          Kevin Spencer
          Microsoft MVP
          Chicken Salad Shooter
          Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.

          >
          A man, a plan, a canal, a palindrome that has.. oh, never mind.
          >
          "Truong Hong Thi" <thi1981@gmail. comwrote in message
          news:1160370247 .045671.246630@ i42g2000cwa.goo glegroups.com.. .

          I would think all of those patterns result in matches. Could it be some
          mistake in your code?

          jayanthigk2004@ yahoo.com wrote:
          Input
          a-
          >
          Patterns
          >
          \w*-*
          \w*(-*)
          \w*(-)*
          \w*[-]*
          >
          No of the patterns results in a match. Tried escaps(\) before hyphen
          still no match.
          >
          My requirement is to make sure a string contains only digits, alphabets
          and hypen (each 0 or more times).

          Comment

          • Kevin Spencer

            #6
            Re: hyphen in regular expression (cSharp)

            I answered your question. See below.

            --
            HTH,

            Kevin Spencer
            Microsoft MVP
            Chicken Salad Shooter
            Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


            A man, a plan, a canal, a palindrome that has.. oh, never mind.

            <jayanthigk2004 @yahoo.comwrote in message
            news:1160409842 .083229.201370@ e3g2000cwe.goog legroups.com...
            My requirement is for validation and it should allow only digits,
            alphabets and hyphens in the text the user enters.
            >
            Thanks
            >
            Kevin Spencer wrote:
            >The patterns would certainly match "a-" but not the requirements he
            >posted.
            >It is a matter of sequence. The regular expression:
            >>
            >\w*-*
            >>
            >literally means "a match is zero or more word characters *followed by*
            >zero
            >or more hyphen characters. So, the string "-a" would result in 2 matches,
            >one for the "-" and one for the "a", while the regular expression:
            >>
            >[\w-]*
            >>
            >literally means "a match is any combination 0 or more of word characters
            >and/or hypens, so that the string "-a" would result in a single match
            >containing both characters.
            >>
            >--
            >HTH,
            >>
            >Kevin Spencer
            >Microsoft MVP
            >Chicken Salad Shooter
            >http://unclechutney.blogspot.com
            >>
            >A man, a plan, a canal, a palindrome that has.. oh, never mind.
            >>
            >"Truong Hong Thi" <thi1981@gmail. comwrote in message
            >news:116037024 7.045671.246630 @i42g2000cwa.go oglegroups.com. ..
            >
            I would think all of those patterns result in matches. Could it be some
            mistake in your code?
            >
            jayanthigk2004@ yahoo.com wrote:
            >Input
            >a-
            >>
            >Patterns
            >>
            >\w*-*
            >\w*(-*)
            >\w*(-)*
            >\w*[-]*
            >>
            >No of the patterns results in a match. Tried escaps(\) before hyphen
            >still no match.
            >>
            >My requirement is to make sure a string contains only digits,
            >alphabets
            >and hypen (each 0 or more times).
            >
            >

            Comment

            Working...