V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ZhangTingkuo
V2EX  ›  问与答

Java 正则表达式问题

  •  
  •   ZhangTingkuo · 2016-05-04 10:35:30 +08:00 · 1740 次点击
    这是一个创建于 3112 天前的主题,其中的信息可能已经有所发展或是发生改变。
    encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB');
    
    headers: {
    	'VerificationToken': 'T1g_69b6CqBB1C0kO7XBYp_H_eVCUjnCU6GB7o3lx7VHCpSgKncivkOQuKPAxR2czgCxBb1ctnA2uK-GsIYijNH_F5Y1:Xn2ZkeeD-Ythul_Y6GxHrOcuW7bLwrUQxwf23E9hNVDPVQupoTzIdyvSch-z2-f3Ay1m8yRiISslUetKVMi3ExT1t3k1'
    },
    

    以上是一段 JavaScript 中的内容,现在要提取PublicKeyVerificationToken的值,也就是

    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB

    T1g_69b6CqBB1C0kO7XBYp_H_eVCUjnCU6GB7o3lx7VHCpSgKncivkOQuKPAxR2czgCxBb1ctnA2uK-GsIYijNH_F5Y1:Xn2ZkeeD-Ythul_Y6GxHrOcuW7bLwrUQxwf23E9hNVDPVQupoTzIdyvSch-z2-f3Ay1m8yRiISslUetKVMi3ExT1t3k1

    正则表达式应该怎么写呢???

    7 条回复    2016-05-05 09:45:25 +08:00
    kindjeff
        1
    kindjeff  
       2016-05-04 10:43:01 +08:00 via iPhone
    正则真不会~~这个直接用单引号分割字符串再取就可以呀
    ZhangTingkuo
        2
    ZhangTingkuo  
    OP
       2016-05-04 10:46:55 +08:00
    @kindjeff 这只是网页中的一段,太长了。。
    Lonely
        3
    Lonely  
       2016-05-04 11:04:02 +08:00
    encrypt\.setPublicKey\('(\w+)'\)
    Lonely
        4
    Lonely  
       2016-05-04 11:17:55 +08:00   ❤️ 1
    headers:\s*{\s*'VerificationToken': '([-\w:_]+)'\s*}
    funsunz
        5
    funsunz  
       2016-05-04 11:28:56 +08:00
    ```java
    String s = "encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB');\n\nheaders: {"
    +"\n'VerificationToken': 'T1g_69b6CqBB1C0kO7XBYp_H_eVCUjnCU6GB7o3lx7VHCpSgKncivkOQuKPAxR2czgCxBb1ctnA2uK-GsIYijNH_F5Y1:Xn2ZkeeD-Ythul_Y6GxHrOcuW7bLwrUQxwf23E9hNVDPVQupoTzIdyvSch-z2-f3Ay1m8yRiISslUetKVMi3ExT1t3k1'"
    +"\n},";
    Matcher m = Pattern.compile("encrypt\\.setPublicKey\\(\\s*'([^']+)'.*\\s*.*VerificationToken'\\s*:\\s*'([^']+)'").matcher(s);
    if (m.find()){
    String publicKey = m.group(1);
    String VerificationToken = m.group(2);
    System.out.println(publicKey);
    System.out.println(VerificationToken);
    }
    ```
    funsunz
        6
    funsunz  
       2016-05-04 11:42:48 +08:00   ❤️ 1
    ZhangTingkuo
        7
    ZhangTingkuo  
    OP
       2016-05-05 09:45:25 +08:00
    @funsunz 多谢,解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2645 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 10:49 · PVG 18:49 · LAX 02:49 · JFK 05:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.