正则表达式以字符串的最后一次出现开始
RegEx to start with the last occurance of the string
我有一个包含数据集的文本文件,我想从中删除一些记录。它看起来像这样:
- =separator=
- data
- unwantedKeyword
- data
- =separator=
- data
- wantedKeyword
- data
- =separator=
- data
- unwantedKeyword
- data
- =separator=
- data
- wantedKeyword
- data
- =separator=
我可以使用某个关键字来识别我不想要(或想要)的记录。我想使用正则表达式利用此关键字和分隔符字符串来删除所有这些表达式。
删除记录的“底部”部分工作正常,但是当我尝试使用以下方法删除“顶部”位时:
=separator=.*?unwantedKeyword
匹配的开始是第一个可用的 =separator=(第 5 行),而不是结束关键字(第 11 行)之前的最后一个(第 9 行),导致需要的记录被删除。
是否可以只匹配该字符串的最后一个实例(模拟数据中的=separator=),所以只匹配第 1. 到 3. 和 9. 到 10. 行而不是 1. 到 3. 和 5.到 11. 会被替换吗?
编辑:
或者我猜只是让它向后读取文件? Notepad++ 为 RegEx 禁用了该选项,所以也不确定这是否可行。
如果你想删除第 1 到 3 行和第 9 到 11 行,你可以使用前瞻来防止匹配所有以 =separator=
开头或包含不需要的关键字的行。
^=separator=.*(?:\R(?!(?:=separator=|.*?\bunwantedKeyword\b)).*)*\R.*?\bunwantedKeyword\b.*\R*
我会用
(?s)=separator=(?:(?!=separator=).)*?unwantedKeyword
参见proof。
说明
--------------------------------------------------------------------------------
(?s) set flags for this block (with . matching \n)
--------------------------------------------------------------------------------
=separator= '=separator='
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the least amount possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
=separator= '=separator='
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character
--------------------------------------------------------------------------------
)*? end of grouping
--------------------------------------------------------------------------------
unwantedKeyword 'unwantedKeyword'
我有一个包含数据集的文本文件,我想从中删除一些记录。它看起来像这样:
- =separator=
- data
- unwantedKeyword
- data
- =separator=
- data
- wantedKeyword
- data
- =separator=
- data
- unwantedKeyword
- data
- =separator=
- data
- wantedKeyword
- data
- =separator=
我可以使用某个关键字来识别我不想要(或想要)的记录。我想使用正则表达式利用此关键字和分隔符字符串来删除所有这些表达式。 删除记录的“底部”部分工作正常,但是当我尝试使用以下方法删除“顶部”位时:
=separator=.*?unwantedKeyword
匹配的开始是第一个可用的 =separator=(第 5 行),而不是结束关键字(第 11 行)之前的最后一个(第 9 行),导致需要的记录被删除。
是否可以只匹配该字符串的最后一个实例(模拟数据中的=separator=),所以只匹配第 1. 到 3. 和 9. 到 10. 行而不是 1. 到 3. 和 5.到 11. 会被替换吗?
编辑:
或者我猜只是让它向后读取文件? Notepad++ 为 RegEx 禁用了该选项,所以也不确定这是否可行。
如果你想删除第 1 到 3 行和第 9 到 11 行,你可以使用前瞻来防止匹配所有以 =separator=
开头或包含不需要的关键字的行。
^=separator=.*(?:\R(?!(?:=separator=|.*?\bunwantedKeyword\b)).*)*\R.*?\bunwantedKeyword\b.*\R*
我会用
(?s)=separator=(?:(?!=separator=).)*?unwantedKeyword
参见proof。
说明
--------------------------------------------------------------------------------
(?s) set flags for this block (with . matching \n)
--------------------------------------------------------------------------------
=separator= '=separator='
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the least amount possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
=separator= '=separator='
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
. any character
--------------------------------------------------------------------------------
)*? end of grouping
--------------------------------------------------------------------------------
unwantedKeyword 'unwantedKeyword'