2

I try to extract the name1 (first-row), name2 (second-row), name3 (third-row) and the street-name (last-row) with regex:

Company Inc.
JohnDoe
Foobar
Industrieterrein 13

The very last row is the street name and this part is already working (the text is stored in the variable "S2").

REGEXREPLACE(S2, "(.*\n)+(?!(.*\n))", "")

This expression will return me the very last line. I am also able the extract the first row:

REGEXREPLACE(S2, "(\n.*)", "")

My problem is, that I do not know how to extract the second and third row....

Also how do I test if the text contains one, two, three or more rows?

Update:

The regex is used in the context of Scribe (a ETL tool). The problem is I can not execute sourcecode, I only have the following functions:

  • REGEXMATCH(input, pattern)
  • REGEXREPLACE(input, pattern, replacement)
4

4 回答 4

6

如果正则表达式语言提供对前瞻的支持,您可以向后计数行,从而得到(假设.不匹配换行符)

(.*)$                   # matching the last line
(.*)(?=(\n.*){1}$)      # matching the second last line (excl. newline)
(.*)(?=(\n.*){2}$)      # matching the third last line (excl. newline)
于 2013-05-28T08:26:48.123 回答
2

只需使用这个正则表达式:

(.+)+

解释:

. 通配符:匹配除 . 之外的任何单个字符\n

+ 匹配前一个元素一次或多次。

于 2013-05-28T08:08:57.827 回答
1

至于将匹配四行中的每一行的正则表达式,如何:

(.*?)\n(.*?)\n(.*?)\n(.*)

括号将匹配,\n 将匹配一个新行。注意:您可能必须使用\r\n而不仅仅是\n依赖;尝试两者。

于 2013-05-28T08:00:16.830 回答
0

您可以尝试以下方法:

((.*?)\n){3}

于 2014-10-23T15:19:04.930 回答