1

I have a statement as follows:

String n =
\ul\insrsid14762702 Symptom}{\insrsid14762702\ul\insrsid14762702 Acid}{\insrsid14762702\ul\insrsid14762702 Nonacid}{\insrsid14762702\ul\insrsid14762702 All}{\insrsid14762702 
 Reg\tab 100%\tab 100%\tab 100%
 Stg pain\tab 100%\tab 83%\tab 100%
 F pain\tab 72%\tab 0%\tab 67%

I would like to see how many tabs there are but I always get zero returned for the size of the arrayList I thought I was adding to. My code:

Pattern patternTabs = Pattern.compile("\\tab",Pattern.DOTALL);
Matcher matcherTabs = patternTabs.matcher(n);

//Add the values between each tab.
ArrayList<String> colValue = new ArrayList<String>();

int count = 0;
while (matcherTabs.find())
    colValue.add(matcher.group());
count++;

System.out.println("ffffffffffffff"+colValue.size());

I have tried \\\\tab \tab tab all other permutations with brackets but no joy.

Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • [Occurrences of substring in a string](http://stackoverflow.com/q/767759) – Tom Apr 17 '16 at 17:35
  • Are you trying to find `tab` word or `\t` space character ? – 11thdimension Apr 17 '16 at 17:38
  • @Sebastian, When asking about whitespace, some reproducible demo at, say, ideone.com, would be really helpful. Or at least the code that can be copy/pasted into the IDE. It is very helpful in general, but is extremely helpful when whitespace is the subject. – Wiktor Stribiżew Apr 17 '16 at 17:59

1 Answers1

1

A literal backslash in a regex requires 4 backslashes in the String literal (2 for regex, doubled again for java), so your regex should be:

"\\\\tab"

But your other "problem" is too much code; just use split():

String[] colValues = n.split("\\\\tab");
Bohemian
  • 412,405
  • 93
  • 575
  • 722