RegEx Either Or

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

    #1

    RegEx Either Or

    I have found much documentation on specific patterns, but I can't find
    anything explaining how to reject strings that match both groups.
    My string could contain specific letters or numbers. If there are
    numbers, then it should NOT have the letters. If it has the letters,
    then it should NOT have the numbers.
    My pattern for the letters is [cCoOhHlL] and numbers is the entire range
    [0-9].

    How can I specify matching one group or the other, and not both?

    Thanks for your help.

    Tom
  • Cor Ligthert [MVP]

    #2
    Re: RegEx Either Or

    Tom,

    Do you mean that you want the link to the regex Bible

    RegexLib


    I hope this helps,

    Cor

    "tomb" <tomb@technetce nter.comschreef in bericht
    news:IDqxg.1835 7$ZH1.3372@bign ews4.bellsouth. net...
    >I have found much documentation on specific patterns, but I can't find
    >anything explaining how to reject strings that match both groups.
    My string could contain specific letters or numbers. If there are
    numbers, then it should NOT have the letters. If it has the letters, then
    it should NOT have the numbers.
    My pattern for the letters is [cCoOhHlL] and numbers is the entire range
    [0-9].
    >
    How can I specify matching one group or the other, and not both?
    >
    Thanks for your help.
    >
    Tom

    Comment

    • Göran Andersson

      #3
      Re: RegEx Either Or

      Just use the | operator:

      ([cCoOhHlL]+|\d+)

      (\d is the same as [0-9].)

      tomb wrote:
      I have found much documentation on specific patterns, but I can't find
      anything explaining how to reject strings that match both groups.
      My string could contain specific letters or numbers. If there are
      numbers, then it should NOT have the letters. If it has the letters,
      then it should NOT have the numbers.
      My pattern for the letters is [cCoOhHlL] and numbers is the entire range
      [0-9].
      >
      How can I specify matching one group or the other, and not both?
      >
      Thanks for your help.
      >
      Tom

      Comment

      • tomb

        #4
        Re: RegEx Either Or

        This allows chars and numbers, but I want either numbers or chars, but
        not both.
        Thanks.

        T

        Göran Andersson wrote:
        Just use the | operator:
        >
        ([cCoOhHlL]+|\d+)
        >
        (\d is the same as [0-9].)
        >
        tomb wrote:
        >
        >I have found much documentation on specific patterns, but I can't
        >find anything explaining how to reject strings that match both groups.
        >My string could contain specific letters or numbers. If there are
        >numbers, then it should NOT have the letters. If it has the letters,
        >then it should NOT have the numbers.
        >My pattern for the letters is [cCoOhHlL] and numbers is the entire
        >range [0-9].
        >>
        >How can I specify matching one group or the other, and not both?
        >>
        >Thanks for your help.
        >>
        >Tom
        >

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: RegEx Either Or

          tomb,
          Try something like:

          [cCoOhHlL]|[0-9]

          The | says match the first group or the second group.

          --
          Hope this helps
          Jay B. Harlow [MVP - Outlook]
          ..NET Application Architect, Enthusiast, & Evangelist
          T.S. Bradley - http://www.tsbradley.net


          "tomb" <tomb@technetce nter.comwrote in message
          news:IDqxg.1835 7$ZH1.3372@bign ews4.bellsouth. net...
          |I have found much documentation on specific patterns, but I can't find
          | anything explaining how to reject strings that match both groups.
          | My string could contain specific letters or numbers. If there are
          | numbers, then it should NOT have the letters. If it has the letters,
          | then it should NOT have the numbers.
          | My pattern for the letters is [cCoOhHlL] and numbers is the entire range
          | [0-9].
          |
          | How can I specify matching one group or the other, and not both?
          |
          | Thanks for your help.
          |
          | Tom


          Comment

          • tomb

            #6
            Re: RegEx Either Or

            Jay B. Harlow [MVP - Outlook] wrote:
            >tomb,
            >Try something like:
            >
            >[cCoOhHlL]|[0-9]
            >
            >The | says match the first group or the second group.
            >
            >
            >
            Yes, but it allows both to be there - I only want one or the other, not
            both.

            Thanks.

            T

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: RegEx Either Or

              Tomb,
              | Yes, but it allows both to be there - I only want one or the other, not
              | both.

              Did you try it? Define specifically what you mean by "I only want one or the
              other"

              Given a 1 character input string, the pattern will match either the first
              pattern or the second pattern. However it will match the pattern.

              Given multiple character input string, the pattern you gave (hence the
              pattern I gave) will still match a single character. It will match the first
              [cCoOhHlL] or the first [0-9]. However! it will still only match a single
              character!


              I suspect by "I only want one or the other" you really want is a sequence of
              the first or a sequence of the second. Plus I suspect that you only want a
              sequence of the first or only a sequence of the second. Something like:

              ^([cCoOhHlL]+|[0-9]+)$

              Which will match a string that contains a sequence of either pattern, but
              only the sequence of a single pattern.

              For example:

              Const pattern As String = "^([cCoOhHlL]+|[0-9]+)$"
              Static parser As New Regex(pattern, RegexOptions.Co mpiled)

              Dim inputs() As String = {"Cohl", "Coh2", "1234", "123c"}
              For Each input As String In inputs
              Debug.WriteLine (parser.Match(i nput).Value, input)
              Next

              Now if you want something else: like the entire string can contain a
              substring of the first or a substring of the second, but only a substring of
              one of the two patterns, you could always apply each pattern individually &
              Xor the Match.Success values. Although I suspect there might be a pattern
              that works, such as:


              ([cCoOhHlL][^0-9])+|([^cCoOhHlL][0-9]+)

              But I have not tested this second pattern... (read I would be surprised it
              works, and will look at it more this evening).

              Expresso:


              RegEx Workbench:


              A tutorial & reference on using regular expressions:
              At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.


              The MSDN's documentation on regular expressions:
              http://msdn.microsoft.com/library/de...geElements.asp

              Expresso & RegEx Workbench are helpful tools for learning regular
              expressions & testing them.

              I use the regular-expressions.inf o as a general regex reference, then fall
              back to MSDN for the specifics. The above link is .NET 1.x; I don't have the
              ..NET 2.0 link handy; not sure if any thing changes in 2.0.


              --
              Hope this helps
              Jay B. Harlow [MVP - Outlook]
              ..NET Application Architect, Enthusiast, & Evangelist
              T.S. Bradley - http://www.tsbradley.net


              "tomb" <tomb@technetce nter.comwrote in message
              news:iZBxg.1719 4$ly.11407@bign ews6.bellsouth. net...
              | Jay B. Harlow [MVP - Outlook] wrote:
              |
              | >tomb,
              | >Try something like:
              | >
              | >[cCoOhHlL]|[0-9]
              | >
              | >The | says match the first group or the second group.
              | >
              | >
              | >
              | Yes, but it allows both to be there - I only want one or the other, not
              | both.
              |
              | Thanks.
              |
              | T


              Comment

              • tomb

                #8
                Re: RegEx Either Or

                You are AMAZING! This stuff is really confusing for a newcomer - the
                one I was looking for is

                ^([cCoOhHlL]+|[0-9]+)$

                It is perfect. Thank you so very much!

                Tom


                Jay B. Harlow [MVP - Outlook] wrote:
                >Tomb,
                >| Yes, but it allows both to be there - I only want one or the other, not
                >| both.
                >
                >Did you try it? Define specifically what you mean by "I only want one or the
                >other"
                >
                >Given a 1 character input string, the pattern will match either the first
                >pattern or the second pattern. However it will match the pattern.
                >
                >Given multiple character input string, the pattern you gave (hence the
                >pattern I gave) will still match a single character. It will match the first
                >[cCoOhHlL] or the first [0-9]. However! it will still only match a single
                >character!
                >
                >
                >I suspect by "I only want one or the other" you really want is a sequence of
                >the first or a sequence of the second. Plus I suspect that you only want a
                >sequence of the first or only a sequence of the second. Something like:
                >
                ^([cCoOhHlL]+|[0-9]+)$
                >
                >Which will match a string that contains a sequence of either pattern, but
                >only the sequence of a single pattern.
                >
                >For example:
                >
                Const pattern As String = "^([cCoOhHlL]+|[0-9]+)$"
                Static parser As New Regex(pattern, RegexOptions.Co mpiled)
                >
                Dim inputs() As String = {"Cohl", "Coh2", "1234", "123c"}
                For Each input As String In inputs
                Debug.WriteLine (parser.Match(i nput).Value, input)
                Next
                >
                >Now if you want something else: like the entire string can contain a
                >substring of the first or a substring of the second, but only a substring of
                >one of the two patterns, you could always apply each pattern individually &
                >Xor the Match.Success values. Although I suspect there might be a pattern
                >that works, such as:
                >
                >
                ([cCoOhHlL][^0-9])+|([^cCoOhHlL][0-9]+)
                >
                >But I have not tested this second pattern... (read I would be surprised it
                >works, and will look at it more this evening).
                >
                >Expresso:
                >http://www.ultrapico.com/Expresso.htm
                >
                >RegEx Workbench:
                >http://www.gotdotnet.com/Community/U...1-4ee2729d7322
                >
                >A tutorial & reference on using regular expressions:
                >http://www.regular-expressions.info/
                >
                >The MSDN's documentation on regular expressions:
                >http://msdn.microsoft.com/library/de...geElements.asp
                >
                >Expresso & RegEx Workbench are helpful tools for learning regular
                >expressions & testing them.
                >
                >I use the regular-expressions.inf o as a general regex reference, then fall
                >back to MSDN for the specifics. The above link is .NET 1.x; I don't have the
                >.NET 2.0 link handy; not sure if any thing changes in 2.0.
                >
                >
                >
                >

                Comment

                • Göran Andersson

                  #9
                  Re: RegEx Either Or

                  No, you are mistaken, it doesn't allow chars and numbers. It allows one
                  or more chars, or one or more numbers, not both.

                  tomb wrote:
                  This allows chars and numbers, but I want either numbers or chars, but
                  not both.
                  Thanks.
                  >
                  T
                  >
                  Göran Andersson wrote:
                  >
                  >Just use the | operator:
                  >>
                  >([cCoOhHlL]+|\d+)
                  >>
                  >(\d is the same as [0-9].)
                  >>
                  >tomb wrote:
                  >>
                  >>I have found much documentation on specific patterns, but I can't
                  >>find anything explaining how to reject strings that match both groups.
                  >>My string could contain specific letters or numbers. If there are
                  >>numbers, then it should NOT have the letters. If it has the letters,
                  >>then it should NOT have the numbers.
                  >>My pattern for the letters is [cCoOhHlL] and numbers is the entire
                  >>range [0-9].
                  >>>
                  >>How can I specify matching one group or the other, and not both?
                  >>>
                  >>Thanks for your help.
                  >>>
                  >>Tom
                  >>

                  Comment

                  Working...