在正则表达式和 Openrefine 中使用 /n 匹配的文本

Text with /n matching in regex and Openrefine

我正在尝试过滤在打开优化中具有 new lines 的文本。

输入为:

Them Spanish girls love me like I'm Aventura
I'm the man, y'all don't get it, do ya?
Type of money, everybody acting like they knew ya
Go Uptown, New York City, bitch
Them Spanish girls love me like I'm Aventura
Tell Uncle Luke I'm out in Miami, too
Them Spanish girls love me like I'm Aventura

预期结果为:

Type of money, everybody acting like they knew ya
Go Uptown, New York City, bitch
Them Spanish girls love me like I'm Aventura

我正在尝试获取带有关键字的行以及前后的行。

我使用标准正则表达式执行此操作的代码如下所示:

/((.*\n){2})^.*\b(New York)\b.*((.*\n){3})/m

但这在开放精炼中不起作用。 我尝试了以下,但只有 returns 'null'

value.match(/.*(\New York)/.*)

有人知道我该怎么做吗? 我真的需要保持线条,所以我不能做 replace(/\n/,'') 赛前。

全新的 OpenRefine 3 has a find() functionmatch() 更加用户友好。

我认为这个正则表达式应该可以解决问题:

value.find(/(.*\n){1}.+New York.+(\n.*){1}/).join('\n')

结果:

如果出于某种原因你更喜欢留在 OpenRefine 2.8,Python/Jython 提供了一个替代方案:

import re
matches = re.findall(r".+?\n.+New York.+\n.+", value)
return "\n".join(matches)

结果:

如果您想完全避免 RegEx 并简单地阅读文本并写下这行之前和之后的行,如果您将文本写在 A1 in [=23= 中的 Cell A1 ]:

Public Sub TestMe()

    Dim inputString As String
    inputString = Range("A1")

    Dim lookForWord As String
    lookForWord = "New York"

    Dim inputArr As Variant
    inputArr = Split(inputString, vbLf)

    Dim line As Variant
    Dim previousLine As String
    Dim foundWord As Boolean
    Dim linesAfter As Long: linesAfter = 1

    For Each line In inputArr
        If InStr(1, line, lookForWord) Then
            previousLine = previousLine & vbCrLf & line
            foundWord = True
        Else
            If foundWord And linesAfter Then
                previousLine = previousLine & vbCrLf & line
                linesAfter = linesAfter - 1
            ElseIf linesAfter Then
                previousLine = line
            End If
        End If
    Next line

    If Not linesAfter Then Debug.Print previousLine

End Sub

Split() 将文本解析为这样的数组:

linesAfter 变量可以告诉您单词后应显示多少行。