我需要從下面的字符串獲得每個塊Foo。我用這個正則表達式。代碼如下。但由於正則表達式的部分($|( ..F))
我只得到Foo[1]
和Foo[3]
。我怎樣才能得到每個Foo
塊與正則表達式?Groovy正則表達式匹配不包含
import java.util.regex.Matcher
String test = ''' ..Foo[1]dsfsdf
...........sfsdfdsfsdfsdf
..............sdfffffffffsd
..................sdffffffffffffffff
..Foo[2]dsfsdf
...........sfsdfdsfsdfsdf
..............sdfffffffffsd
..................sdffffffffffffffff
..Foo[3]dsfsdf
...........sfsdfdsfsdfsdf
..............sdfffffffffsd
..................sdffffffffffffffff
..Foo[4]dsfsdf
...........sfsdfdsfsdfsdf
..............sdfffffffffsd
..................sdffffffffffffffff'''
Matcher m = test =~ /(Foo\[[0-9]{1,6}\][\s\S]*?)($|( ..F))/
m.find();
//after this m.count equals 2 and contains only Foo[1] and Foo[3], but I need 4 with all Foo's
這不是我明白 –
@YassinHajaj爲什麼? Groovy語法? Java等效:Pattern p = Pattern.compile(「(Foo \ [[0-9] {1,6} \] [\ s \ S] *?)($ |(..F))」); Matcher m = p.matcher(test); m.find() –
好吧,我沒有注意到這是groovy。抱歉。 –