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

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.
Software ProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subrat (C++ windows/Linux)Software EngineerCommented:
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
0
Software ProgrammerAuthor Commented:
Can you please help me with the exact regular expression pattern with sample code snippet ?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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;

Open in new window


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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

awking00Information Technology SpecialistCommented:
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.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@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.
0
awking00Information Technology SpecialistCommented:
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
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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++;
        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.