2016-04-06 65 views
-2

我拿出像 val pattern = "(\\w+)\\|(.*)\\|\\[(.*)\\]\\|\"(.*)\"\\|\"(.*)\"\\|\\[(.*)\\]\\|\\[(.*)\\]\\|(.*)\\|\\[(.*)\\]\\|\\[(.*)\\]".r如何分割字符串階用正則表達式

的模式,我有一個原始的字符串

var str = """AuthLogout|vmlxapp21a|[13/Jan/2016:16:33:15 +0100]|"66.77.444.44 uid=XXXXX,ou=People,o=Bank,o=External,dc=xxxx,dc=com"|"abcd_123_portalweb_w "|[]|[41]||[]|[]""" 

然後應用模式的字符串,但它始終是空的。

val items = pattern.findAllIn(str).toList

+0

不要逃避「\」括號'[]'和'ORS | '。 –

回答

2

如果我理解你想要做什麼,也許是使用正則表達式巨人是不是最簡單的方法:通過|可以split,擺脫不必要的分隔符([]"的使用replaceAll):

val str = """AuthLogout|vmlxapp21a|[13/Jan/2016:16:33:15 +0100]|"66.77.444.44 uid=XXXXX,ou=People,o=Bank,o=External,dc=xxxx,dc=com"|"abcd_123_portalweb_w "|[]|[41]||[]|[]""" 
val withoutBoundaries = str.replaceAll("[\"\\]\\[]","") 
val result = withoutBoundaries.split("\\|") 
result.foreach(println) 

它打印:

AuthLogout 
vmlxapp21a 
13/Jan/2016:16:33:15 +0100 
66.77.444.44 uid=XXXXX,ou=People,o=Bank,o=External,dc=xxxx,dc=com 
abcd_123_portalweb_w 

41 

如果想在這裏使用正則表達式,我創建子正則表達式乏表示你後不同的文本部分,使這個有點管理:

val plain = "(.*)"    // no boundary characters 
val boxed = s"\\[$plain\\]"  // same, encapsulated by square brackets 
val quoted = '"' + plain + '"' // same, encapsulated by double quotes 

// the whole thing, separated by pipes: 
val r = s"$plain\\|$plain\\|$boxed\\|$quoted\\|$quoted\\|$boxed\\|$boxed\\|$plain\\|$boxed\\|$boxed".r 

val result = r.findAllIn(str).toList // this list has one item, as expected. 

現在,如果你希望看到這個正則表達式的樣子,在這裏它是 - 但我不建議在你的代碼有這個...:

val r = """(.*)\|(.*)\|\[(.*)\]\|"(.*)"\|"(.*)"\|\[(.*)\]\|\[(.*)\]\|(.*)\|\[(.*)\]\|\[(.*)\]""".r 
相關問題