I am trying to solve the following problem.
Given a string and a Regular Expression pattern, give the number of the times the pattern occurs in the string. RegEx symbols mean as follows:
. - 2 occurrences of the previous character,
+ - 4 occurrences of previous character,
* – more than 5 occurrences of the previous character
Sample Input given:
aaaaaannndnnnnnnfffhfhhgjjjwkkkllclc
a.
n+
a*
an.
a.d.
Sample Output given:
5
3
1
1
0
My approach is to convert all the RegEx to normal pattern. i.e., for the above example my RegEx would be:
aa
nnnn
aaaaaa
ann
aadd
and then count the occurrences. But I am clueless what to do if the input RegEx is:
a*d.
Please note that I cannot use any inbuilt functions like Pattern.Matches. Any suggestions?
Thank you.
*is unbound (any number larger than 5), so you can't actually make the pattern explicit, sincea*is an infinite set of finite strings. Besides, whoever arbitrarily redefined+and*should be aware she is likely to cause much confusion.