簡體   English   中英

需要有關Preg_replace正則表達式的幫助

[英]need help with Preg_replace Regular expressions

這些單詞1和單詞2放在方括號中

要刪除整行取決於word2

[word1] something-line-text [word2] some text again

想要用另一個替換某些文本取決於word2

[word1] something-line-text [word2] some text again

into

REPLCACEDTEXT something-line-text some text again

some line text (something/something)

into

(something/something) some line text

我不是100%不確定您要什么,但這將查找[word1]和[word2]並僅在它們都在線時將其刪除:

$line = preg_replace('/word1(.*)word2/', '$1', $line);

此方法可以匹配部分單詞,並且可能會留有多余的空格。 稍微好一點的是:

$line = preg_replace('/\s?word1\b(.*)\s?word2\b/', '$1', $line);

這樣可以確保使用單詞邊界和空格將它們作為整個單詞進行匹配。 我在一側使用空格,以便占用額外的空間。 如果您不在乎,只需在每個單詞的兩邊使用\\b

第二種情況,將第一個帶括號的短語移至行首:

$line = preg_replace('/(.*)(\(.*?\))/', '$2 $1', $line);

編輯:

看完您的示例后,我認為您想要:

$line = preg_replace('/^\[Scene\].*/', '', $line);

$line = preg_replace('/\[\.*?\]\s+(.*)(\[.*?\])/', '$2 $1');

$line = preg_replace('/(.*)\s+(\(.*?\))/', '$2 $1');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM