如何检查句子中的模式

时间:2016-03-04 05:50:17

标签: java

  

检查模式是否与句子匹配。换句话说,检查模式中的所有单词是否与模式的顺序相同。如果模式与此匹配,则方法返回模式单词之前,之间和之后的短语。如果模式不匹配,则返回null。

有人可以帮我找出这项任务的最佳算法吗?这是在Java中创建一个新方法。

2 个答案:

答案 0 :(得分:1)

您想要做的是使用Lookahead和Lookbehind,如下所示。

import com.google.api.client.util.Base64;
import com.google.gson.JsonObject;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.Signature;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Enumeration;

public class TestJWT {

    private final static Charset UTF8_CHARSET = Charset.forName("UTF-8");
    private static KeyStore myStore = null;
    private static FileInputStream in_cert = null;

    public static void main(String[] args) {
        PrivateKey privateKey = null;
        try {
            in_cert = new FileInputStream(
                    "example.p12");


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            myStore = KeyStore.getInstance("PKCS12");
            myStore.load(in_cert, "notasecret".toCharArray());
            String alias = "";
            Enumeration objEnumeration = myStore.aliases();
            while (objEnumeration.hasMoreElements()) {
                alias = (String) objEnumeration.nextElement();
                privateKey = (PrivateKey) myStore.getKey(alias,
                        "notasecret".toCharArray());

            }
            System.out.print(privateKey);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        JsonObject header = new JsonObject();
        header.addProperty("alg", "RS256");
        header.addProperty("typ", "JWT");

        Calendar cal = Calendar.getInstance();
        cal.set(1970, 01, 01);
        String iat = Long.toString((System.currentTimeMillis() - cal.getTimeInMillis()) / 1000);
        String exp = Long.toString((System.currentTimeMillis() - cal.getTimeInMillis()) / 1000 + 60000L);

        JsonObject claim = new JsonObject();
        claim.addProperty("iss", "xxxxxxxxx.com");
        claim.addProperty("scope", "https://www.googleapis.com/auth/bigquery https://www.googleapis.com/auth/cloud-platform");
        claim.addProperty("aud", "https://www.googleapis.com/oauth2/v4/token");
        claim.addProperty("exp", exp);
        claim.addProperty("iat", iat);


        System.out.println("Header : " + header);
        String headerStr = header.toString();
        System.out.println("claim : " + claim);
        String claimStr = claim.toString();


        try {

            byte[] headerArr = headerStr.getBytes(UTF8_CHARSET);
//            System.out.println(Base64.encodeBase64String(headerArr));

            byte[] claimArr = claimStr.getBytes(UTF8_CHARSET);
//            System.out.println(Base64.encodeBase64String(claimArr));

            String inputStr = Base64.encodeBase64String(headerArr) + "." + Base64.encodeBase64String(claimArr);

//            System.out.println("Input String : " + inputStr);
            Signature signature = Signature.getInstance("SHA256withRSA");



            signature.initSign(privateKey);
            signature.update(inputStr.getBytes(UTF8_CHARSET));
            System.out.println("Sign : " + Arrays.toString(signature.sign()));

            System.out.println("Base64url encoded sign : " + Base64.encodeBase64String(signature.sign()));

            System.out.println("Final JWT : " + Base64.encodeBase64String(headerArr) + "." + Base64.encodeBase64String(claimArr) + "." + Base64.encodeBase64String(signature.sign()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

您可以使用

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

功能。将短语存储在字符串中,比如str。然后使用if语句

select table1.column_number 
from table1 
left join table2 on table1.column_number  =table2.column_number 
where table2.column_number  is null

接下来是你的手术。