我有許多以下程式碼區塊的實例,經過簡化:
$image = '';
$name = '';
$link = '';
$role_suffix = '';
$role_name = '';
$notes = '';
所有這些變數都包含不同的數據。我需要將文件轉換為數組,我可以通過簡單的搜索和替換來完成此操作,但首先我需要$link
使用$image
.因此:
$link = '';
$name = '';
$image = '';
$role_suffix = '';
$role_name = '';
$notes = '';
當然必須有一個正規表示式解決方案來保存手動更改它們?我嘗試過將不同問題中找到的答案組合在一起,但正則表達式對我來說根本沒有意義!我嘗試過這個(<div>.*?</div>)(\s+)(<span>.*?</span>)
並替換為\3\2\1
但我不確定正確的語法。
答案1
- Ctrl+H
- 找什麼:
(\$image\h*=\h*.+?;)([\s\S]+?)(\$link\h*=\h*.+?;)
- 用。
$3$2$1
- 檢查環繞
- 檢查正規表示式
- 取消選取
. matches newline
- Replace all
解釋:
( # start group 1
\$image # literally
\h* # 0 or more horizontal spaces
= # equal sign
\h* # 0 or more horizontal spaces
.+? # 1 or more any character but newline, not greedy
; # semicolon
) # end group 1
( # start group 2
[\s\S]+? # 1 or more any character, not greedy
) # end group 2
( # start group 3
\$link # literally
\h* # 0 or more horizontal spaces
= # equal sign
\h* # 0 or more horizontal spaces
.+? # 1 or more any character but newline, not greedy
; # semicolon
) # end group 3
螢幕截圖: