match string in JAVA

I have a string written as …YYK0*.

<.> (dot) – single occurrence of the character (a dot can be replaced by any character)

<*> (asterisk) – match any string of characters

right now I want to check whether a input string like TTIYYK0AA includes the string YYK0 besides the letter Y must begin to show in the index 3 in the input string.

That is to say, if the input string is TTIIYYK0AA or TTYYK0AA, it will return false even though it includes the string YYK0.

Besides the index is not always equal 3, it depends the number of dot.
If the string is …..YYK0* , that means the sub-string YYK0 begins to show in the index of 5 in the input string.

How could I realize that function.

Please help and thank U guys!

UPDATED: Original answer (included at the end) was based on a misinterpretation of the question, a misinterpretation everybody else seems to have made too.

From comment:

It depends on the number of <.> (dot). If the string is …..YYK0* in the database, it means that the string YYK0 must begin to show in the index of 5 in the input string……

So, you’re reading the pattern to match against from the database, and you want to turn that pattern into some function that can be used to test strings in Java.

From your text, it would appear that a . can match any single character, and that a * can match any sequence of characters.

In that case, converting the pattern from the database into a regular expression would likely be the best way to go.

The . pattern is the same for regex, i.e. a ..
The * pattern would be .* in regex.
Everything else would need to be escaped (quoted), so it gets matched literally.

You can use this method to convert that pattern into a Java regex:

private static Pattern toRegex(String patternFromDb) {
    StringBuffer regex = new StringBuffer();
    Matcher m = Pattern.compile("//.{1,}|//*{1,}|[^.*]+").matcher(patternFromDb);
    while (m.find()) {
        String s = m.group();
        switch (s.charAt(0)) {
            case '.':
                // leave dots as they are
                break;
            case '*':
                m.appendReplacement(regex, ".*"); // replace * with .*
                break;
            default:
                m.appendReplacement(regex, Matcher.quoteReplacement(s)); // escape literal match
        }
    }
    return Pattern.compile(m.appendTail(regex).toString());
}

You can then use the returned Pattern object like this:

Pattern p = toRegex("...YYK0*");

if (p.matcher("TTIYYK0AA").matches()) {
    // code here
}

Original Answer

You say “position 4”, but your example shows “index 3”, so it would appear that your “position” is 1-based.

In Java, you can do the test using:

if (str.indexOf("YYK0") == 3)

In SQL, you can do the test using:

WHERE col LIKE '___YYK0%'
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HelloWorld{

     public static void main( String args[] ) {
      // String to find the pattern.
      String line = "TTIYYK0AA";
      String pattern = "^.{3}YYK0";

      // Creating a Pattern object
      Pattern r = Pattern.compile(pattern);

      // Creating Matcher object
      Matcher m = r.matcher(line);
      if (m.find( )) {
         System.out.println("MATCHED");
      }else {
         System.out.println("NO MATCH");
      }
   }
}

In Java we can simply use string.indexof() method to get your requirement,
please find the below code which will be helpful.

public class PatternMatch 
{
    public static void main(String[] args) 
    {
        String str = "TTIYYK0AA";
       if(str.indexOf("YYK0") == 3)
        {
            System.out.println("Success");
            //write what ever your want..
        }
        else
        {
            System.out.println("Fail");
            //write what ever your want
        }
    }
}

Thank you.

Luckily, your pattern string is similar to a regex.

Therefore you can do a slight conversion from your pattern to a regex pattern, and simply use String regex matching in Java.

The conversion you need is changing * to .* (because you want * to represent any string)

so the function look like (pseudo-code):

boolean match(String string, String pattern) {
    String regexPattern = pattern.replace("//*", ".*");
    return string.matches(regexPattern);
}