I have a requirement wherein I need to search a string if it has a word repeated thrice consecutively (for example: hello are are are you there, this should be treated as hello are you there). Is there any way by which I can treat the word "are" as a regex. What I mean is to treat the variable as regex. Or is there any other better way to implement this thing ?
Thanks for the response. But the string there in the example demonstrated by you is a constant one. While in my case the String will be fetched from database, hence it can be anything. So, how do I choose the regex( "are are are" in your case) dynamically.
You need to use a capturing group, and then check if the contents of that group reappear. Allowing only whitespace between the words:
The (\\w+) part captures one single word. The \\s+ means one or more occurrences of whitespace. The \\1 means the exact same value as the captured word.
Replace the \\s+ with something else to also allow other characters; for instance, [\\s,]+ also allows commas.
Thanks for your suggestion and that works absolutely fine when I have a string of type "Hi how are are are you". But it doesnt return anything when I try for strings like "Hi How, are, are, are, you". How do I handle such cases.
If it's two or more you use a qualifier for that: "(\\w+)(\\s+\\1)+". The whitespace + repetition then is required one or more times.
If you want the comma inside the match, add that to the \\w+: "(\\w+,?). The ? makes the comma optional.
However, that will give problems with cases like "I, I, I am". The last "I" does not match the starting "I,", so replacing would give you "I, I am". Putting the comma with the whitespace (as I had already mentioned) will solve that; "I, I, I am" will become "I am", and "I, I, I, am" will become "I, am" because the last comma is not part of the match.