473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 25 '06 #1
8 1886
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

"tomb" <to**@technetcenter.comschreef in bericht
news:ID******************@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

Jul 25 '06 #2
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
Jul 25 '06 #3
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
Jul 25 '06 #4
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" <to**@technetcenter.comwrote in message
news:ID******************@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
Jul 25 '06 #5
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
Jul 26 '06 #6
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/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.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
"tomb" <to**@technetcenter.comwrote in message
news:iZ******************@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
Jul 26 '06 #7
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.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/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.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.

Jul 26 '06 #8
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
Jul 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
11
by: proctor | last post by:
hello, i have a regex: rx_test = re.compile('/x()*x/') which is part of this test program: ============ import re
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
4
by: CJ | last post by:
Is this the format to parse a string and return the value between the item? Regex pRE = new Regex("<File_Name>.*>(?<insideText>.*)</File_Name>"); I am trying to parse this string. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.