如何在两个特殊字符串之间替换特殊字符。
我有这样的事情:
"start 1
2-
G
23
end"
我希望得到以下内容:
"start 1 2- G 23 end"
仅在“开始和结束”之间用空格替换\ n
Test1;Hello;"Text with more words";123
Test2;want;"start
1-
76 end";123
Test3;Test;"It's a test";123
Test4;Hellp;"start
1234
good-
the end";1234
Test5;Test;"It's a test";123
在notepad ++中有可能吗?
答案 0 :(得分:4)
您可以使用此模式:
(?:\G(?!\A)|\bstart\b)(?:(?!\bend\b).)*\K\R
细节:
(?:
\G(?!\A) # contiguous to a previous match
|
\bstart\b # this is the first branch that matches
)
(?:(?!\bend\b).)* # zero or more chars that are not a newline nor the start of the word "end"
\K # remove all on the left from the match result
\R # any newline sequence (\n or \r\n or \r)
注意:(?:(?!\bend\b).)*
效率不高,可以根据您的具体情况更好地替换它。
答案 1 :(得分:0)
魔术词是懒惰量词,前瞻和单行模式。
PHP
(使用PCRE)的解决方案是:
<?php
$string = __your_string_here__;
$regex = '~(?s)(?:start)(?<content>.*?)(?=end)(?s-)~';
# ~ delimiter
# (?s) starts single line mode - aka dot matches everything
# (?:start) captures start literally
# .*? matches everything lazily
# (?=end) positive lookahead
# (?s-) turn single line mode off
# ~ delimiter
preg_match_all($regex, $string, $matches);
$content = str_replace("\n", '', $matches["content"][1]);
echo $content; // 1234good-the
?>