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