How to find occurrences of a substring="\r" in a string="This is a \\rtest \n\r string" in java using Pattern and matchers.
When I use
Pattern p4=Pattern.compile("\\r",Pattern.LITERAL);
Even then this is not working
It worked well when I have string="This is a \\rtest \\n\\r string".It have me correct count i.e..2
But for string="This is a \\\\\rtest \\\n\\\r string"
.
It gave me incorrect count =1;
function to be called:
static int countMatches(Pattern pattern, String string)
{
Matcher matcher=pattern.matcher(string);
int count=0;
int pos=0;
while(matcher.find(pos))
{
count++;
pos=matcher.start()+1;
}
return count;
}