0

I want to check one long string contain multiple string.

I am trying to use below command.

 String[] words = {"GAGGAG", "AGGAC"};
 Pattern pattern = Pattern.compile("GAGGAG|AGGAC");
 if(pattern.matcher("GAGGAGGTC").find()){
 System.out.println("find");
 }else{
 System.out.println("Not find");
 }

Results supposed to be Not Find because "GAGGAGGTC" contain "GAGGAG" but does not contain "AGGAC"

How can I give option from "or" to "And"

And There is one more option.

 String[] words = {"GAGGAG", "AGGAC"};
 Pattern pattern = Pattern.compile("GAGGAG|AGGAC");
 if(pattern.matcher("GAGGAGGAC").find()){
 System.out.println("find");
 }else{
 System.out.println("Not find");
 } 

This is also should show "Not find". Because There is not allowing overlap part. "GAGGAG" and "AGGAC" is overlapping "AG" part from "GAGGAGGAAC"

VLAZ
29.5k9 gold badges65 silver badges87 bronze badges
asked Jan 16, 2015 at 22:41
2
  • 1
    Do you have to use regular expressions? Using contains is much simpler. Commented Jan 16, 2015 at 22:44
  • 1
    This might help you stackoverflow.com/questions/469913/… Commented Jan 16, 2015 at 22:44

3 Answers 3

2

You must need to use a alternation operator | like below.

Pattern.compile("GAGGAG.*AGGAC|AGGAC.*GAGGAG");

Explanation:

  • GAGGAG.*AGGAC Matches the GAGGAG plus .* any character would present in-between and must have a AGGAC substring.

  • | OR operator, so that it would match any order.

  • AGGAC matches AGGAC , .* zero or more characters plus GAGGAG string.

Example 1:

 Pattern pattern = Pattern.compile("GAGGAG.*AGGAC|AGGAC.*GAGGAG");
 if(pattern.matcher("GAGGAGGAC").find()){
 System.out.println("find");
 }else{
 System.out.println("Not find");
 } // Output: Not find

Example 2:

Pattern pattern = Pattern.compile("GAGGAG.*AGGAC|AGGAC.*GAGGAG");
 if(pattern.matcher("GAGGAGAGGAC").find()){
 System.out.println("find");
 }else{
 System.out.println("Not find");
 } 
} // Output: find

Example 3:

Pattern pattern = Pattern.compile("GAGGAG.*AGGAC|AGGAC.*GAGGAG");
 if(pattern.matcher("AGGACFOOGAGGAG").find()){
 System.out.println("find");
 }else{
 System.out.println("Not find");
 } // Output: find
answered Jan 16, 2015 at 23:46

Comments

1

You don't need a regex for that purpose.

Use String#contains:

public boolean checkContainsAll(String sentence, String[] words) {
 for(String word : words) {
 if(!sentence.contains(word)) {
 return false;
 }
 }
 return true;
}

In your example:

String[] words = {"GAGGAG", "AGGAC"};
String sentence = "GAGGAGGTC";
if(checkContainsAll(sentence, words)) {
 System.out.println("The sentence " + sentence + " contains all words");
} else {
 System.out.println("The sentence " + sentence +" does not contain all words.");
}

DEMO


UPDATE

To check that there is no overlapping, the simplest solution in my example would be to remove the words if they are found in the given sentence, so that they will not be present for next checks:

public boolean checkContainsAll(String sentence, String[] words) {
 for(String word : words) {
 if(!sentence.contains(word)) {
 return false;
 }
 sentence = sentence.replace(word, "");
 }
 return true;
}

DEMO

answered Jan 16, 2015 at 23:10

2 Comments

I am really sorry it's great but I forgot to mention since begging that does not allow overlapping part.
I mean sentence "GAGGAGGTC" not allow because there is overlapping part. but "GAGGAGNNNAGGAC" allow because it's not overlapping
0

Change your regex to this for a "and" operator

(?=GAGGAG)(?=AGGAC)
answered Jan 16, 2015 at 22:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.