我有一個String
的路由項:解析字符串與正則表達式
"key modifier path.to.routing.key;"
"key"
是始終存在的(我並不需要存儲這部分)"modifier"
(字)可以出現0或更多次(我需要存儲修飾符)"path.to.routing.key"
是實際的鍵,並且將始終是該行的最後一個組件,後跟一個「;」可能有0個或更多''。'字符在路由鍵但始終至少1個字(我需要存儲這部分)。
該字符串將在自己的行中與其他非有趣的文本[所以我不能只使用String.split(" ");
]。我打算使用regex
Pattern
s到它弄出來使用:
Pattern p = Pattern.compile("...pattern...");
進出口新到Java regex
,並可能與一些幫助做,
如何使用Pattern
到單獨的這個String
到它的組件?
,如果有幫助的一些例子:
key public direct routing.key;
key direct routingkey;
等等,問題是如何使用正則表達式來查找文件中的行或如何使用正則表達式將行分割成您必須存儲的組件?如果後者 - 爲什麼使用正則表達式? – Fildor 2014-09-20 14:17:51
「該字符串將在其他文件中的其他非有趣的文字自己的行[所以我不能只使用String.split(」「);]」這我覺得矛盾。如果字符串是在它自己的文本行中,爲什麼不能在「」上分割? – Fildor 2014-09-20 14:19:48
@Fildor這兩個,我用正則表達式作爲在文件的其餘部分非有趣的文字可能包含單詞鍵等 – Edd 2014-09-20 14:20:41