Home All Groups Group Topic Archive Search About
Author
25 Jul 2006 3:20 PM
tomb
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

Author
25 Jul 2006 3:41 PM
Cor Ligthert [MVP]
Tom,

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

RegexLib
http://www.regexlib.com/Default.aspx

I hope this helps,

Cor

Show quoteHide quote
"tomb" <t***@technetcenter.com> schreef in bericht
news:IDqxg.18357$ZH1.3372@bignews4.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
Author
25 Jul 2006 8:47 PM
Göran_Andersson
Just use the | operator:

([cCoOhHlL]+|\d+)

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

tomb wrote:
Show quoteHide quote
> 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
Author
25 Jul 2006 9:54 PM
tomb
This allows chars and numbers, but I want either numbers or chars, but
not both.
Thanks.

T

Göran Andersson wrote:

Show quoteHide quote
> 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
>
Author
27 Jul 2006 10:42 AM
Göran_Andersson
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:
Show quoteHide quote
> 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
>>
Author
26 Jul 2006 12:01 AM
Jay B. Harlow [MVP - Outlook]
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


Show quoteHide quote
"tomb" <t***@technetcenter.com> wrote in message
news:IDqxg.18357$ZH1.3372@bignews4.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
Author
26 Jul 2006 4:10 AM
tomb
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
Author
26 Jul 2006 12:54 PM
Jay B. Harlow [MVP - Outlook]
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.Compiled)

        Dim inputs() As String = {"Cohl", "Coh2", "1234", "123c"}
        For Each input As String In inputs
            Debug.WriteLine(parser.Match(input).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/UserSamples/Details.aspx?SampleGuid=c712f2df-b026-4d58-8961-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/default.asp?url=/library/en-us/cpgenref/html/cpconRegularExpressionsLanguageElements.asp

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

I use the regular-expressions.info 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


Show quoteHide quote
"tomb" <t***@technetcenter.com> wrote in message
news:iZBxg.17194$ly.11407@bignews6.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
Author
26 Jul 2006 1:25 PM
tomb
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:

Show quoteHide quote
>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.Compiled)
>
>        Dim inputs() As String = {"Cohl", "Coh2", "1234", "123c"}
>        For Each input As String In inputs
>            Debug.WriteLine(parser.Match(input).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/UserSamples/Details.aspx?SampleGuid=c712f2df-b026-4d58-8961-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/default.asp?url=/library/en-us/cpgenref/html/cpconRegularExpressionsLanguageElements.asp
>
>Expresso & RegEx Workbench are helpful tools for learning regular
>expressions & testing them.
>
>I use the regular-expressions.info 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.
>
>

>