我有一個像這樣的字串: "abcd abcd | abcde | Degree SP | xyz abcd | abcd ABC"
我需要"Degree SP"使用正則運算式提取。我怎樣才能做到這一點?這里的條件是:
- 以“SP”結尾的字串
- 字串從最后一個“|”開始。
我正在嘗試使用 Google Sheet 公式REGEXEXTRACT(<input string>, "[\|\s]. SR[\s\|]")
它回傳" | abcde | Degree SP "。如何限制從最后一個“|”中提取 ?
uj5u.com熱心網友回復:
如果字串Degree SP應該在管道和空格之間:
\|\s([^\s|][^|]*SP)\s\|
\|\s匹配|和一個空白字符(捕獲組 1[^\s|]匹配除空格以外的單個字符或|[^|]*SP匹配除|SP之外的可選字符并匹配 SP
)關閉第 1 組\s\|匹配一個空白字符和|

如果只有之后的管道Degree SP是強制性的:
([^\s|][^|]*SP)\s*\|

uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下正則運算式。
^.*?\s \S \s \|\s \S \s \|\s ([^\\|]*)\s \|.*$
上述正則運算式的在線演示
或者您想捕獲|以SP字串結尾的第 2 次和第 3 次出現之間的值,然后嘗試以下正則運算式:
^.*?\s \S \s \|\s \S \s \|\s ([^\\|]*SP)\s \|.*$
上述正則運算式的在線演示
說明:為以上添加詳細說明。
^.*?\s \S \s ##Matching from starting of value with a lazy match till 1st occurrence of spaces followed by 1 or more non-spaces followed by 1 or more spaces.
\|\s \S \s \| ##Matching |(literal) followed by spaces followed by 1 or more non-spaces followed by spaces with |(literal character) here.
\s ##Matching 1 or more spaces occurrences here.
([^\\|]*) ##Creating 1 and only capturing group which has everything till next occurrence of | to get Degree SP value mentioned by OP in samples.
\s \|.*$ ##Matching 1 or spaces followed by | till last of value/line.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/358669.html
