我对regex不太满意(我花了几个小时),而且我很难替换2个标识符(“ {|”和“ |}”)之间的所有空行
我的正则表达式如下(对不起,您的眼睛):(\{\|)((?:(?!\|\}).)+)(?:\n\n)((?:(?!\|\}).)+)(\|\})
(\{\|)
:字符“ {|” ((?:(?!\|\}).)+)
:如果不是在“ |}”之后的所有内容(负向超前)(?:\n\n)
:我要删除的空行((?:(?!\|\}).)+)
:如果不是在“ |}”之后的所有内容(负向超前)(\|\})
:字符“ |}” 它可以工作,但是只删除最后一个空行,您能帮我使它与所有空行一起工作吗?
我尝试在\ n \ n上添加否定的前瞻性,并在所有内容上都使用重复组,但此方法无效。
答案 0 :(得分:3)
几种方法:
基于\G
的模式:(仅需要一个模式)
$txt = preg_replace('~ (?: \G (?!\A) | \Q{|\E ) [^|\n]*+ (?s: (?! \Q|}\E | \n\n) . [^|\n]*)*+ \n \K \n+ ~x', '', $txt);
\G
匹配字符串的开头或最后一次成功匹配后字符串中的位置。这样可以确保多个匹配是连续的。
我所说的一种基于\G
的模式可以如下所示:
(?: \G position after a successful match | first match beginning ) reach the target \K target
“达到目标” 部分的目的是永远不匹配结束序列|}
。因此,找到最后一个目标后,\G
部分将失败,直到第一个匹配部分再次成功。
~
### The beginning
(?:
\G (?!\A) # contigous to a successful match
|
\Q{|\E # opening sequence
#; note that you can add `[^{]* (*SKIP)` before to quickly avoid
#; all failing positions
#; note that if you want to check that the opening sequence is followed by
#; a closing sequence (without an other opening sequence), you can do it
#; here using a lookahead
)
### lets reach the target
#; note that all this part can also be written like that `(?s:(?!\|}|\n\n).)*`
#; or `(?s:[^|\n]|(?!\|}|\n\n).)*`, but I choosed the unrolled pattern that is
#; more efficient.
[^|\n]*+ # all that isn't a pipe or a newline
# eventually a character that isn't the start of |} or \n\n
(?s:
(?! \Q|}\E | \n\n ) # negative lookahead
. # the character
[^|\n]*
)*+
#; adding a `(*SKIP)` here can also be usefull if there's no more empty lines
#; until the closing sequence
### The target
\n \K \n+ # the \K is a conveniant way to define the start of the returned match
# result, this way, only \n+ is replaced (with nothing)
~x
或preg_replace_callback
:(更简单)
$txt = preg_replace_callback('~\Q{|\E .*? \Q|}\E~sx', function ($m) {
return preg_replace('~\n+~', "\n", $m[0]);
}, $txt);
答案 1 :(得分:2)
您可以使用正向前行模式来确保匹配的空白行后跟|}
,也可以使用负向前行模式来确保空白行和{{1}之间没有字符}是|}
的起始位置:
{|
答案 2 :(得分:1)
如果您使用:
library!WindowsService_9!14c0!07/11/2019-10:18:01:: i INFO: Schedule 3d1ac939-3fb6-4313-9e04-bce6353a6825 executed at 07/11/2019 10:18:01.
schedule!WindowsService_9!14c0!07/11/2019-10:18:01:: Creating Time based subscription notification for subscription: a9d6e90a-84fc-439d-b715-49f7479cf69d
library!WindowsService_9!14c0!07/11/2019-10:18:01:: i INFO: Schedule 3d1ac939-3fb6-4313-9e04-bce6353a6825 execution completed at 07/11/2019 10:18:01.
library!WindowsService_9!14c0!07/11/2019-10:18:01:: e ERROR: Throwing Microsoft.ReportingServices.Diagnostics.Utilities.ServerConfigurationErrorException: AuthzInitializeContextFromSid: Win32 error: 1722, Microsoft.ReportingServices.Diagnostics.Utilities.ServerConfigurationErrorException: The report server has encountered a configuration error. ;
processing!WindowsService_9!14c0!07/11/2019-10:18:01:: e ERROR: An exception has occurred in data set 'DS_Example'. Details: Microsoft.ReportingServices.Diagnostics.Utilities.ServerConfigurationErrorException: The report server has encountered a configuration error.
processing!WindowsService_9!14c0!07/11/2019-10:18:01:: i INFO: DataPrefetch abort handler called for Report with ID=. Aborting data sources ...
processing!WindowsService_9!14c0!07/11/2019-10:18:01:: e ERROR: Throwing Microsoft.ReportingServices.ReportProcessing.ProcessingAbortedException: , Microsoft.ReportingServices.ReportProcessing.ProcessingAbortedException: An error has occurred during report processing. ---> Microsoft.ReportingServices.Diagnostics.Utilities.ServerConfigurationErrorException: The report server has encountered a configuration error.
--- End of inner exception stack trace ---;
processing!WindowsService_9!14c0!07/11/2019-10:18:01:: w WARN: Data source ' Data source for shared dataset': Report processing has been aborted.
processing!WindowsService_9!14c0!07/11/2019-10:18:01:: e ERROR: Throwing Microsoft.ReportingServices.ReportProcessing.ProcessingAbortedException: , Microsoft.ReportingServices.ReportProcessing.ProcessingAbortedException: An error has occurred during report processing. ---> Microsoft.ReportingServices.Diagnostics.Utilities.ServerConfigurationErrorException: The report server has encountered a configuration error.
--- End of inner exception stack trace ---;
library!WindowsService_9!14c0!07/11/2019-10:18:01:: i INFO: Initializing EnableExecutionLogging to 'True' as specified in Server system properties.
notification!WindowsService_9!14c0!07/11/2019-10:18:01:: e ERROR: Error occured processing subscription a9d6e90a-84fc-439d-b715-49f7479cf69d: An error has occurred during report processing.
然后它将匹配在(?<={\|)(\n{2,}|(\r\n){2,}|\s+)(?=\|})
和{|
之间找到的新行和空白