user580459
user580459

Reputation: 53

regex to find substring

Say I have a substring BB which may be alone or part of a longer string, e.g. BB or AA|BB|CC or BB|CC or AA|BB i.e. if it follows/is followed by another substring it MUST be separated by a |. What regex do i need to find BB in any of the above but not in say AABB?

Upvotes: 5

Views: 655

Answers (4)

Alan Moore
Alan Moore

Reputation: 75242

Here's another option:

Pattern p = Pattern.compile("(?<![^|])BB(?![^|])");

String[] input = { "AABB", "BB", "AA|BB|CC", "BB|CC", "AA|BBB", "BBB|AA" };
for (String s : input)
{
  Matcher m = p.matcher(s);
  System.out.printf("%-10s : %b%n", s, m.find() );
}

output:

AABB       : false
BB         : true
AA|BB|CC   : true
BB|CC      : true
AA|BBB     : false
BBB|AA     : false

This is effectively the same as @Kobi's answer, but where he's saying the BB IS preceded/followed by a pipe or the beginning/end of the string, I'm making the equivalent assertion that it's NOT preceded/followed a character that's NOT a pipe.

Upvotes: 3

Kaj
Kaj

Reputation: 10959

Don't know if it is the position that you want, but this captures where BB starts and ends, if BB is followed by '|' or ends of string:

String data = "AA|BB|CCBBCC|BB";
Matcher m = Pattern.compile("(BB)(?:\\||$)").matcher(data);
while (m.find()) {
    System.out.println(m.group(1) + " starts at " + m.start() + " ends at " + m.end(1));
}

Upvotes: 0

Kobi
Kobi

Reputation: 138117

If your substrings are limited to alphanumeric characters, you ca use:

\bBB\b

If they don't, you can simulate the same using lookarounds:

(?<=\||^)BB(?=\||$)

Your substring should be before and after a pipe, or near the edges.

Upvotes: 4

Chris Eberle
Chris Eberle

Reputation: 48795

I think this will do it:

^(.+[|])?BB([|].+)?$

And after testing here I'm going to say yes, this is it.

Upvotes: 6

Related Questions