-3

Checks to see if the pattern matches the sentence. In other words,checks to see that all the words in the pattern are in the sentence in the same order as the pattern. If the pattern matches then this, method returns the phrases before, between and after the pattern words. If the pattern does not match then null is returned.

Can someone help me to figure out the best algorithm for this assignment? This is creating a new method in Java.

2 Answers2

1

What you would want to do is use Lookahead and Lookbehind as shown below.

private static String[] splitRegex(String input, String regex) {
    String[] splitString=  input.split("((?<=" + regex + ")|(?=" + regex + "))");
    if (splitString.length>1){
        return splitString;
    }else {
        return new String[]{};
    }
}
Ritesh Shakya
  • 575
  • 5
  • 17
0

You can use

    contains() 

Function. Store the phrase in the string, say str. Then use if statement

    if(str.contains("<variable(s)>"))

Followed by your operation.

Sanket Gupte
  • 357
  • 1
  • 4
  • 16