2015-09-30 58 views
2

我正在使用Jake Wharton的DiskLRUCache,並且遇到了創建密鑰並將它們格式化爲庫特定正則表達式的問題。正則表達式非法參數異常

1)根據圖書館,我的鑰匙必須符合[a-z0-9 _-] {1,64}

2)我使用格式化我的鑰匙代碼:

String key = url.replace(".", "-") 
      .replaceAll("^[a-z0-9_-]", "") 
      .toLowerCase(); 

if(key.length() > 63) 
    key = key.substring(key.length() - 63, key.length()); 

Log.d(TAG, "key: " + key); 

DiskLruCache.Snapshot snapshot = mDiskCache.get(key); 

3)代碼DiskLRUCache使用來驗證我的鑰匙

static final String STRING_KEY_PATTERN = "[a-z0-9_-]{1,120}"; 
static final Pattern LEGAL_KEY_PATTERN = Pattern.compile(STRING_KEY_PATTERN); 

private void validateKey(String key) { 
    Matcher matcher = LEGAL_KEY_PATTERN.matcher(key); 
    if (!matcher.matches()) { 
     throw new IllegalArgumentException("keys must match regex " 
       + STRING_KEY_PATTERN + ": \"" + key + "\""); 
    } 
    } 

4)這裏有一些按鍵的我的用戶越來越被扔IAException:

54 | 00:00:02:227 | D/asset manager key: a9e9b46-e476-fdd1rx0y0w500h325r_330000_000-mp4 
59 | 00:00:07:132 | D/asset manager key: 5f54b52-2ef9-3420rx0y0w135h156r_00cc00_120-mp4 
64 | 00:00:08:665 | D/asset manager key: 6c705f7-c0b8-1235rx0y0w395h314r_000033_240-mp4 
69 | 00:00:08:984 | D/asset manager key: 7cdb2b3-f0f7-2c76rx0y0w500h153r_cccccc_000-mp4 
74 | 00:00:09:477 | D/asset manager key: 5f54b52-2ef9-3420rx0y0w135h156r_00cc00_120-mp4 
79 | 00:00:14:999 | D/asset manager key: a91dcba-07a4-58ccrx0y0w227h199r_663300_040-mp4 
84 | 00:00:18:176 | D/asset manager key: 5aad806-70a0-6426rx0y0w500h200r_333300_060-mp4 
89 | 00:00:25:499 | D/asset manager key: 8e7b5b5-eb57-2f6erx0y0w500h333r_cccc99_060-mp4 
94 | 00:00:26:262 | D/asset manager key: a1fa904-3f87-4ee7rx0y0w498h281r_330066_280-mp4 
99 | 00:00:29:370 | D/asset manager key: a1fa904-b01f-8ca8rx0y0w404h273r_663333_000-mp4 
104 | 00:00:33:981 | D/asset manager key: 0d8298c-7922-8914rx0y0w242h140r_3366cc_220-mp4 
109 | 00:00:36:839 | D/asset manager key: 2577a4b-88ea-8282rx0y0w500h372r_333300_060-mp4 
114 | 00:00:40:294 | D/asset manager key: 6c20180-1454-b7e4rx0y0w500h273r_330066_280-mp4 
119 | 00:00:44:582 | D/asset manager key: toilet_flush_ani_ef1405f6-c75a-aa6f-a5f29f1085e8a151-gif 
124 | 00:00:45:149 | D/asset manager key: a5097fb-7f16-fea5rx0y0w299h296r_996633_040-mp4 
129 | 00:00:48:491 | D/asset manager key: 25773d1-7499-5fecrx0y0w500h210r_996633_040-mp4 
134 | 00:00:52:509 | D/asset manager key: 2e78ac2-e675-95b5rx0y0w500h281r_cccccc_000-mp4 
139 | 00:00:55:684 | D/asset manager key: a91dcba-c029-01a1rx0y0w380h177r_663300_040-mp4 
144 | 00:00:56:848 | D/asset manager key: a215a90-996d-850erx0y0w500h250r_cccccc_000-mp4 
196 | 00:00:57:476 | D/asset manager key: 82369cc-0a2f-4be8rx0y0w280h210r_330000_000-mp4 
201 | 00:00:58:393 | D/asset manager key: a1fa904-09fe-25berx0y0w320h240r_330000_000-mp4 
206 | 00:01:05:755 | D/asset manager key: a3f8891-f017-cf00rx0y0w500h250r_cc9933_040-mp4 
211 | 00:01:07:640 | D/asset manager key: 6890e6e-4c44-fa27rx0y0w500h325r_006600_120-mp4 
216 | 00:01:10:147 | D/asset manager key: a215a90-2e33-ab4erx0y0w500h375r_996633_040-mp4 
221 | 00:01:12:938 | D/asset manager key: 2e78ac2-5541-5c23rx0y0w500h243r_cccccc_000-mp4 
226 | 00:01:14:499 | D/asset manager key: 02a9b27-3e20-4a6drx0y0w325h429r_cccccc_000-mp4 
231 | 00:01:16:610 | D/asset manager key: a9534a9-2abc-572frx0y0w500h349r_333300_060-mp4 
236 | 00:01:18:263 | D/asset manager key: a9f17f7-dceb-46fbrx0y0w50h50r_cc9933_040-mp4 
+1

而問題是......? – async

+0

在這組鍵上拋出了IAException,並且我不確定原因。 2)中提供的代碼是否不足以使我的密鑰與1)中提到的模式匹配? – Android

+0

您可以發佈STRING_KEY_PATTERN的值是什麼?!? – d0nut

回答

2

這個問題似乎與所用送的正則表達式模式密鑰。

當您使用.replaceAll("^[a-z0-9_-]", "")時,它將刪除字符串中的第一個符號:字母或數字或下劃線或連字符。

要刪除除本類以外的所有字符,請使用否定的字符類 - 只需將插入符的左方括號之後:

String key = url.replace(".", "-") 
       .replaceAll("[^a-z0-9_-]", "");