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"
3 Answers 3
You must need to use a alternation
operator |
like below.
Pattern.compile("GAGGAG.*AGGAC|AGGAC.*GAGGAG");
Explanation:
GAGGAG.*AGGAC
Matches theGAGGAG
plus.*
any character would present in-between and must have aAGGAC
substring.|
OR operator, so that it would match any order.AGGAC
matches AGGAC ,.*
zero or more characters plusGAGGAG
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
Comments
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.");
}
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;
}
2 Comments
Change your regex to this for a "and" operator
(?=GAGGAG)(?=AGGAC)
contains
is much simpler.