Results 1 to 5 of 5

Thread: [2.0] Regex - find a match NOT located between two chars

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Question [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'

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Regex - find a match NOT located between two chars

    Am I in the wrong place for Regular Expressions help?

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2.0] Regex - find a match NOT located between two chars

    I think it should be
    (?<!b)a+(?!b)

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Regex - find a match NOT located between two chars

    Quote 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.

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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
  •  



Click Here to Expand Forum to Full Width