# Count the number of times the delimiter occurs by escaping the delimiter in a special case

Software Programmer used Ask the Experts™
on
Count the number of times the delimiter occurs by escaping the delimiter in a special case

I have read the entire content of a file as String. Now i want to identify the number of times of a delimiter found in the entire string..However it should not be followed by a question mark and we should ignore them.

example

Number of occurances found of \$ = 1 in the below FTX+AAA+++201707141009UTC?:??\$QASCO UK LTS;?:V.D??\$ TOUZA?:GB?:GB\$

Number of occurances found of \$ = 2 in the below FTX+AAA+++201707141009UTC?:??\$QASCO UK LTS;?:V.D??\$ TOUZA?:GB?:GB\$ABCD\$

Number of occurances found of \$ = 3 in the below FTX+AAA+++201707141009UTC?:??\$QASCO UK LTS;?:V.D??\$ TOUZA?:GB?:GB\$ABCD\$PPP?\$QQQQ\$

Please let me know how to achieve this in java.

Thanks.
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer

Commented:
You can solve it using regular expression. (https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html)
Use Pattern and Matcher classes.
1. Check how many ?\$ are there. => Store in variable (No_Of_QD)
2. Check total no of \$    => Store in variable (NO_OF_D)
3. Count =  NO_OF_D - No_Of_QD

Commented:
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Here is a one-liner to get what you want (note this might not be the most efficient method, if efficiency is important let us know and I can modify slightly)...

``````int count = "FTX+AAA+++201707141009UTC?:??\$QASCO UK LTS;?:V.D??\$ TOUZA?:GB?:GB\$".split("(?<!\\?)\\\$", -1).length - 1;
``````

Explanation: the regex uses a negative look-behind to find just the delimiter \$ (it excludes those that are preceded by the ? ). Then it splits the original string based on this delimiter into an array and gets the number of elements in the array, minus 1. The minus 1 is because a string with, for example, 2 delimiters will split into 3 strings, so we take one away to get the answer.
Information Technology Specialist

Commented:
Can you explain your example results? In the first string, there are three dollar signs, one followed by a Q, one followed by a T (that displays as being on the next line) and one followed by nothing (it's a the end of the line), yet you say the answer should be 1. In the third string, there are five dollar signs followed by a letter and not a question mark, yet you say the answer should be 3.
IT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
@awking00,

I think it's just a language thing, and they meant to say "should not be preceded by a ?"

Must say that I didn't even notice this when I read the question, because it is pretty obvious from the examples and the question was referring to"escape" characters which generally always come before the character being escaped.
Information Technology Specialist

Commented:
The simplest way is probably to use apache.commons.lang.String Utils,countMatches method (or countOccurrences in later versions)
String = "FTX+AAA+++201707141009UTC?:??\$QASCO UK LTS;?:V.D??\$ TOUZA?:GB?:GB\$ABCD\$";
String delim = "\$";
String find = "?\$";
int count = StringUtils.countMatches(str,delim) - StringUtils.countMatches(str,find);
System.out.println("Output: + count";

Output: 2
IT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
I'm not sure if I would say that adding a library dependency just for doing this is the "simplest" way of doing it.

The other alternative to the esoteric regex that I posted above, is a simple old, easy to understand loop...

``````        String input = "FTX+AAA+++201707141009UTC?:??\$QASCO UK LTS;?:V.D??\$ TOUZA?:GB?:GB\$";
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '\$' && (i == 0 || input.charAt(i - 1) != '?'))
count++;
}
``````

Do more with