1

I am new to regex and help me with a solution

for example i have a string as below,

String str = "this is a hello world string and duplicate starts from here this is a hello world string";

i wanted to check the following condition using regex.

if("this is a hello world string" has appeared more than once in String str){
    return false;
}
else{
    return true;
}

How this can be achieved?

4
  • 1
    Why do you insist on using a regex for this? Commented Nov 13, 2013 at 10:40
  • This is a bit tricky to get correct unless you have a fixed set of strings which are needed to be verified against duplicates. You can even match the word 'and' couple of times in the String and mark it as duplicate. Commented Nov 13, 2013 at 10:41
  • @NPE Without using regex is also fine. Commented Nov 13, 2013 at 10:41
  • if (str.split("this is a hello world string").length > 1) { .. } Commented Nov 13, 2013 at 11:27

4 Answers 4

1

You may also use regex like in this example:

String str1 = "this is a hello world string";
String str2 = "this is a hello world string and duplicate starts from here this is a hello world string";
Pattern pattern = Pattern.compile(str1);
Matcher matcher = pattern.matcher(str2);

int count = 0;

while(matcher.find()){
    count++;
}

if(count > 0) {
     return true;
} else {
     return false;
}

Hope it helps. Cheers.

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

2 Comments

It is checking if the pattern has the string "this is a hello world string". But what i wanted is different
@Manoj You are right, I've changed this answer for You. Please see also this site: docs.oracle.com/javase/tutorial/essential/regex/matcher.html
1

You can do it without a regex like this

if(str.indexOf("this is a hello world string") != str.lastIndexOf("this is a hello world string")) { 
    return false;
}
else{
    return true;
}

2 Comments

I didn't downvote your answer, but where you have s.lastIndexOf did you want str.lastIndexOf ?
@user2777005 - Thanks for pointing that out. I fixed that typo(though you're very much welcome to edit my answer as well)! Yet I feel its not something this minor which attracted the downvote. Anyways, its up to the OP, if they prefer this answer or not! :)
0

You can use below code

String duplicatePattern = "(?i)\\b(\\w+)\\b[\\w\\W]*\\b\\1\\b";
    Pattern p = Pattern.compile(duplicatePattern);
    String phrase = "this is a hello world string and duplicate starts from here this is a hello world string";
    Matcher m = p.matcher(phrase);
    String val = null;
    while (m.find()) {
        val = m.group();
        System.out.println("Matching segment is \"" + val + "\"");
        System.out.println("Duplicate word: " + m.group(1)+ "\n");
    }

1 Comment

your snippes gives the output as Matching segment is "this is a hello world string and duplicate starts from here this" Duplicate word: this but it doesn check for the duplicate string
0

If you literally want to find two duplicate "things" without specifying what those things are, you're going to have a hard time writing up the regex for it.

This matches two "duplicate things":

(.+)(?=.+)\1

Note, it finds "anything" and asserts that "anything else" can be in between it and another instance of itself.

Yeah, like that isn't confusing.

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.