没有包含Groovy正则表达式匹配

时间:2016-04-02 20:20:41

标签: regex groovy

我需要从下面的字符串中获取每个块Foo。我使用正则表达式。代码如下。但是由于正则表达式的($|( ..F))部分,我只得到Foo[1]Foo[3]。如何使用正则表达式获取每个Foo块?

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

1 个答案:

答案 0 :(得分:0)

您只需要在($|( ..F))开启?=之后添加(,将(Foo\[[0-9]{1,6}\][\s\S]*?)(?=$| \.\.F) ^^^^^^^^^^^^^ 变为fill = TRUE非消费者(并且避开点可以匹配文字点):

data = read.table(file.choose(),sep=",",fill=TRUE)

data[is.na(data)] <- ""   # Replacing NA Values with nothing..

请参阅positive lookahead