1

I'd like to know which regex I must use. The code inside a method is:

while( (line = bReader.readLine()) != null){
    line2 = line.replaceAll("[\\)][\\|]","R");
    numbers = line2.split("[\\|]");
}
int num = numbers.length;

What I want is that when line equals

(A#,A#,A#),(B#,B#,C#),(B#,B#,C#),(Bb,Bb,Cb)|(Ab,Ab,Ab),(Bb,Bb,Cb),(Bb,Bb,Cb),(Bb,Bb,Cb)|

it must return num = 0 because all instances of )| are replaced by R and there is no | left. What I get is num = 1.

When line equals

(A#,A#,A#),(B#,B#,C#),(B#,B#,C#),(Bb,Bb,Cb)|A#,B#,C#,D#, E#,F#,G#,  |  ,A,  , ,   ,  ,  ,  ,  ,  ,  , ,   ,  ,  ,  |

it must return num = 2 because there are two instances of | after replacing the )| by R. What I get here is indeed num = 2. I hope someone can give me the solution.

4
  • 2
    line2.split("[\\|]"); escaping here is redundant as the meta character is already in character class. line2.split("[|]"); is fine . :) Commented Mar 25, 2013 at 17:33
  • 1
    I didn't see any problem, the first return 1, because the array has only one element, the string with "R". the 2nd has two elements, so number=2. Commented Mar 25, 2013 at 17:39
  • 2
    When you split on a character that doesn't exist you will get the original String back in a one element Array. More generally if you split a String with one delimiter in it you get back an Array size 2, one with two delimiters will give back an Array size 3. See a pattern here? Commented Mar 25, 2013 at 17:40
  • I expect that the first string returns 0 because it has only instances of )| which are replaced by R. 0 is what I want to get. Commented Mar 25, 2013 at 17:44

2 Answers 2

1

If you are trying to find out how many | marks exists in String that are not predicted by ) then you can remove these marks and check how length of string changed. To detect such pipes you can use negative look-behind.

int num = s.length() - s.replaceAll("(?<![)])[|]", "").length();
Sign up to request clarification or add additional context in comments.

1 Comment

Very neat - didn't think of just comparing lengths after replaceAll. +1
0

If you split a String on a delimiter that doesn't exist then you will get back the original String:

public static void main(String[] args) throws SQLException {
    System.out.println(Arrays.toString("My string without pipes".split("\\|")));
}

Output:

[My string without pipes]

If you try and split on a character that the string ends with you do not get an empty String in the Array:

public static void main(String[] args) throws SQLException {
    System.out.println(Arrays.toString("My string ending in pipe|".split("\\|")));
}

Output:

[My string ending in pipe]

All that happens is the delimiter at the end is dropped.

So your logic is wrong. The reason you got the correct answer in your second check is not because the check is correct but because the pipe happened to be at the end.

Generally, you will not get the number of delimiters in a String using spilt, you will get the number +1 unless your String starts or ends with the delimiter - in which case it will simply be dropped.

What you need to do is use regex to search for all pipes not preceded by a closing parenthesis. You can do this with negative lookbehind:

public static void main(String[] args) throws SQLException {
    final String s1 = "(A#,A#,A#),(B#,B#,C#),(B#,B#,C#),(Bb,Bb,Cb)|(Ab,Ab,Ab),(Bb,Bb,Cb),(Bb,Bb,Cb),(Bb,Bb,Cb)|";
    final String s2 = "(A#,A#,A#),(B#,B#,C#),(B#,B#,C#),(Bb,Bb,Cb)|A#,B#,C#,D#, E#,F#,G#,  |  ,A,  , ,   ,  ,  ,  ,  ,  ,  , ,   ,  ,  ,  |";
    final Pattern pattern = Pattern.compile("(?<!\\))\\|");
    int count = 0;
    final Matcher matcher = pattern.matcher(s1);
    while (matcher.find()) {
        ++count;
    }
    System.out.println(count);
    count = 0;
    matcher.reset(s2);
    while (matcher.find()) {
        ++count;
    }
    System.out.println(count);
}

Output:

0
2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.