0

pattern in java

i need to write a code in java where it matches a string taken as input with a pattern . The pattern may start with ". " or "H ". After encoutering the first H it should be followed by any number of times "." and then "T". thus the following patterns are valid patterns .........H.......T......H......T H.......T.....H......T..... ......HT......H....T... .....H......TH......T i want to write the code for pattern i.e. string pattern= ??? plz help.

24th May 2017, 8:58 AM
NIKHIL MURARKA
3 Answers
+ 1
you may be looking for regular expressions. for your particular question, "[\.H]\.*T" could be the one you are after.
24th May 2017, 9:08 AM
Venkatesh Pitta
Venkatesh Pitta - avatar
0
@Venkatesh Pitta thanks for ur answer.it helped me a lot to reach the final answer. But can u plz let me why u put \.H under the third bracket.I think it would work without that also.and i think it made a slight mistake in your wonderful answer it should be [\\.H]\\.*T.
24th May 2017, 4:25 PM
NIKHIL MURARKA
0
@NIKHIL MURARKA a . by itself stands for any character. So it has to be escaped with a \. Glad to be of help. I wrote the answer before putting it through the compiler, so mistakes are likely.
25th May 2017, 1:09 AM
Venkatesh Pitta
Venkatesh Pitta - avatar