|
-
Dec 12th, 2006, 10:47 AM
#1
Thread Starter
Frenzied Member
[2.0] Regex - find a match NOT located between two chars
Let's say I have this line of text:
aaa c b aa b ax
and I want to match all strings of a's not located between matched b's so in this case the matches would be {aaa, a}
I'm trying this regular expression but it doesn't work:
(?<!b.*)a+(?!.*b)
I'm trying to tell it to match all strings of a's except "those preceded by a b and followed by a b."
What this expression really says is match all strings of a's except "those preceded by a b OR followed by a b."
aaa - followed by a b
aa - preceded and followed by a b
a - preceded by a b
I'm wondering if there's a way to specify both 'not preceded by' and 'not followed by'
-
Dec 13th, 2006, 10:47 AM
#2
Thread Starter
Frenzied Member
Re: [2.0] Regex - find a match NOT located between two chars
Am I in the wrong place for Regular Expressions help?
-
Dec 13th, 2006, 12:22 PM
#3
Re: [2.0] Regex - find a match NOT located between two chars
I think it should be
(?<!b)a+(?!b)
-
Dec 13th, 2006, 02:15 PM
#4
Thread Starter
Frenzied Member
Re: [2.0] Regex - find a match NOT located between two chars
 Originally Posted by wild_bill
I think it should be
(?<!b)a+(?!b)
Unfortunately, that won't work either. I probably didn't make it clear what I want to find. I want to find a's not located anywhere between matching b's, not just right in front of or right behind a 'b'.
Consider this input string:
Code:
ab a b b a baaa
^ this 'a' should be found but won't since a 'b' directly follows it
^ this 'a' will be found even though it shouldn't since it is
between two matching 'b' but isn't directly preceded or followed by a 'b'.
^ same as above
^ only the last two 'a' will be found since the first 'a'
is followed by a 'b'.
Last edited by wey97; Dec 13th, 2006 at 02:18 PM.
-
Dec 13th, 2006, 05:12 PM
#5
Re: [2.0] Regex - find a match NOT located between two chars
Code:
string noBs = Regex.Replace("ab a b b a baaa","b.*?b","");
foreach (Match mt in Regex.Matches(noBs,"a+"))
{
Console.WriteLine(mt.Value);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|