473,569 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression - Match All Except

In my continuing inability to completely understand regular expressions I
have a new one for you.

I'd like to capture a string "A" unless it is anywhere in between string "B"
and string "C".

Therefore some matches are:

XYZAHIJ
ABC
BCA

Non matches include:

BAC
BXYZAHIJC
BAXYZC

Also, since I'm trying to replace "A" and only "A" the expression needs to
exclude the rest of the match. I assume that means using non-capturing
groups, but again, they and I don't mix.

Thank you,
Derek Stone
EliteVB.com
Jul 21 '05 #1
3 18742
(^[^BC]+A[^BC]+$)|(^A.*)|(.*A $)

This says:

match one of three patterns - they are each enclosed by () and separated
by | in the above
the first pattern (^[^BC]+A[^BC]+$) says:
(^ = insist on beginning of line at the start of the match
$) = insist on end-of-line for the end of the match
[^BC] = matches anything except B or C.
[^BC]+ = match a string of chars that is anything except B or C
and of course the A in the middle is the target you are matching
the 2nd pattern (^A.*) says:
match any line that begins with A: (^A
the 3rd pattern is similar, it says
match any line that ends in A : A$)
Suggestion:
Get a visual regex designer, it's a simple tool that makes things much
easier.
One I use and can recommend is Regex, freebie download from organicbit.com:
http://www.organicbit.com/regex/fog0000000019.html

--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ o n l i n e . m i c r o s o f t . c o m
"Derek Stone" <ds****@elitevb .com> wrote in message
news:eV******** *****@TK2MSFTNG P10.phx.gbl...
In my continuing inability to completely understand regular expressions I
have a new one for you.

I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C".

Therefore some matches are:

XYZAHIJ
ABC
BCA

Non matches include:

BAC
BXYZAHIJC
BAXYZC

Also, since I'm trying to replace "A" and only "A" the expression needs to
exclude the rest of the match. I assume that means using non-capturing
groups, but again, they and I don't mix.

Thank you,
Derek Stone
EliteVB.com

Jul 21 '05 #2
Try this:

Regex regex = new Regex(@"
^ # beginning of string
(?!B.*A.*C) # don't match B, followed by A, followed by C
.*A.* # match A anywhere
$ # end of string",
RegexOptions.Ex plicitCapture |
RegexOptions.Co mpiled |
RegexOptions.Si ngleline |
RegexOptions.Ig norePatternWhit espace);

It uses a zero-width lookahead that says "if you match B, followed by A,
followed by C", the whole match should fail.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Derek Stone" <ds****@elitevb .com> wrote in message
news:eV******** *****@TK2MSFTNG P10.phx.gbl...
In my continuing inability to completely understand regular expressions I
have a new one for you.

I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C".

Therefore some matches are:

XYZAHIJ
ABC
BCA

Non matches include:

BAC
BXYZAHIJC
BAXYZC

Also, since I'm trying to replace "A" and only "A" the expression needs to
exclude the rest of the match. I assume that means using non-capturing
groups, but again, they and I don't mix.

Thank you,
Derek Stone
EliteVB.com

Jul 21 '05 #3
Thank you gentlemen. That was most helpful, although I do have a follow-up.

Eric: I was using an expression similar to yours however it doesn't span
across multiple lines.

Example:

text
B
text
A
text C

The "A" should -not- match, however it does with both my original expression
and yours (due to the line breaks). Granted I didn't provide an example in
my question to cover such a case, so I have no one except myself to blame
for that. How do I account for this? I've tried using both a
RegexOptions.Mu ltiline and RegexOptions.Si ngleline configuration, with no
luck.

In addition I do use a regular expressions utility, Expresso.

Regards,
Derek Stone
EliteVB.com
Jul 21 '05 #4

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

Similar topics

1
4157
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) ...
3
2008
by: Tom | last post by:
I have struggled with the issue of whether or not to use Regular Expressions for a long time now, and after implementing many text manipulating solutions both ways, I've found that writing specialized code instead of an RE is almost always the better solution. Here is why.... RE's are complex. Sure it is one line of code, but it is on...
3
290
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". Therefore some matches are: XYZAHIJ ABC
5
2273
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate each pharse? test case: "I love" all "of you" I would like it return:
25
5137
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access...
11
3088
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
5
2303
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex = qr/hello(?{print "saw hello\n"})mello(?{print "saw mello\n"})/; "jellohellomello" =~ /$regex/;
6
9041
by: Peter Duniho | last post by:
So, I'm trying to learn how the Regex class works, and I've been trying to use it to do what I think ought to be simple things. Except I can't figure out how to do everything I want. :( If I want to take a string and break it into individual lines based on a specific pattern ("\r\n" in this case, but I don't think it matters), I can...
1
4804
by: Mr.SpOOn | last post by:
Hi, I've never used exception before, but I think now it's time to start. I've seen that there is a list of the built-in exceptions in the Python docs, but this explains the meaning of every exception. Does exist an inverted list? I mean, how may I know what kind of exception is going to raise my code? Shall I figure out by myself? ...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
1
7679
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...
1
5514
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...
0
5223
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...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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
0
946
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...

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.