RegEx Either Or

T

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Just use the | operator:

([cCoOhHlL]+|\d+)

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

tomb

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

T

Göran Andersson said:
Just use the | operator:

([cCoOhHlL]+|\d+)

(\d is the same as [0-9].)
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
 
J

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


|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
 
T

tomb

Jay said:
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
 
J

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/...mpleGuid=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/d...l/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


| 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
 
T

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
 
?

=?ISO-8859-1?Q?G=F6ran_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.
This allows chars and numbers, but I want either numbers or chars, but
not both.
Thanks.

T

Göran Andersson said:
Just use the | operator:

([cCoOhHlL]+|\d+)

(\d is the same as [0-9].)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top