473,387 Members | 1,569 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.

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 18728
(^[^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*************@TK2MSFTNGP10.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.ExplicitCapture |
RegexOptions.Compiled |
RegexOptions.Singleline |
RegexOptions.IgnorePatternWhitespace);

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*************@TK2MSFTNGP10.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.Multiline and RegexOptions.Singleline 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
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...
3
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...
3
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". ...
5
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...
25
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...
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...
5
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 =...
6
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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,...
0
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...

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.