0

So I have a bonus task assigned and it asks to write a program which returns true if in a given string at least one character is repeated.

I am relatively new to regular expressions but to my knowledge this should work:

String input = "wool";
return input.matches(".*(.)/1+.*");

This should return true, because the '.*' at the beginning and the end express that there could be prefices or suffices. And the '(.)/1+' is a repeating pattern of any character.

As I said I'm relatively new to the regex stuff but I'm very interested in learning and understanding it.

3 Answers 3

2

Almost perfect, just / looks the wrong way around (should be \).

Also, you don't need .* for prefixes and suffixes - regexp will find a match anywhere in the string, so (.)\1 suffices. This is not an error, just an optimisation (although in other cases it might, and does, make a difference).

One more issue is that backslashes are special characters in Java strings, so when you write a regexp in Java, you need to double up on backslashes. This gives you:

return input.matches(".*(.)\\1.*");

EDIT: I forgot, you don't need + because if something repeats 3 times, it also repeats 2 times, so you will find it just by searching for a two-character repetition. Again, not an error, just not needed here.

And Kita has a good point that your task is not well-defined, as it does not say whether you are looking for the repeating characters next to each other or anywhere in the string. My solution is for the adjacent characters; if you need the repetition anywhere, use his.

EDIT2 after comments: Forgot the semantics of .matches. You guys are quite correct, edited appropriately.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but for some reason removing the '.*' results in wrong answers. My guess is that the pattern has to fit exactly and not just partially. And "(.)\\1" is only matching to a string which repeats the same character twice. But doubling up the bachslashes solves the problem.
input.matches("(.)\\1"); This won't work in Java, since String.matches() makes sure the regex matches the whole line. Search for existence of a pattern in a String can only be done by Matcher.find(). input.matches(".*(.)\\1.*"); is what the OP needs.
0

If the task "in a given string at least one character is repeated" includes the following pattern:

  • abcbd (b is repeated)

then the regex pattern would be:

(.).*\1

This pattern assumes that other characters could be in between the repeating characters. Otherwise

(.)\1

will do.

Note that the task is to capture "at least one character is repeated", which means identifying a single occurrence is enough for the task, so \1 does not have to have + quantifier.

The code:

return input.matches("(.).*\\1");

or

return input.matches("(.)\\1");

Comments

0

Alternative solution would be adding the elements to hashset. Then checking the length of the string and the hashset.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.