473,769 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1906
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**@technetce nter.comschreef in bericht
news:ID******** **********@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

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**@technetce nter.comwrote in message
news:ID******** **********@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
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.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.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"tomb" <to**@technetce nter.comwrote in message
news:iZ******** **********@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
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.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.

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
5729
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 is to improve, but not to prove.
7
2618
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) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
7
1914
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 starting and ending with a *. I tried things like string regex = @"(^*\|*^)"); but it still doesn't work completely (matches on "*test" too for example). If anything could help me with this, that would be appreciated.
17
3979
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 http://forta.com/books/0672325667/
11
3111
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 a hand? import regsub
6
2503
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 the right parts in there in one big line and for some reason that did not work either. Here is my regex's. static Regex rar = new Regex("\\.part.*", RegexOptions.IgnoreCase); static Regex par = new Regex("\\.vol.*", RegexOptions.IgnoreCase);
11
279
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
2066
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 Artist - Title (Remix) Artist - Title
4
2672
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. <File_Name>Services</File_Name> Thanks
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.