[英]Notepad++ RegEx to only capture text without certain preceding word
[英]How to capture text between a specific word and the semicolon immediately preceding it with regex?
我在 Excel 中有很多行人和頭銜,我希望按頭銜過濾掉某些人。 例如,單元格可能包含以下內容:
John Smith, Co-Founder;Jane Doe, CEO;James Jackson, Co-Founder
這些單元格的長度不同,並且具有不同數量的人員和頭銜。 我的計划是在開頭和結尾添加分號以使其標准化。 這會給我:
;John Smith, Co-Founder;Jane Doe, CEO;James Jackson, Co-Founder;
目前,我有一個可以迭代並使用以下正則表達式Founder.*?;
的代碼; 它將根據我的代碼(即Founder;Founder;
)返回創始人的每個實例,但問題是我似乎無法弄清楚如何同時捕獲人的姓名。 我想我需要在“Founder”之前指定分號,但到目前為止我還沒有得到這個。 我的最終目標是返回類似於以下內容的內容,除了正確的正則表達式之外,我有代碼。
;John Smith, Co-Founder;James Jackson, Co-Founder;
根據您的 Excel 版本,您還可以使用以下公式執行此操作:
=FILTERXML("<t><s>" & SUBSTITUTE(A1,";","</s><s>")&"</s></t>","//s[contains(.,'Co-Founder')]")
但是,對於正則表達式,您可以使用
(?:^|;)([^;]*?Co-Founder)
這將返回捕獲組 1 中的聯合創始人。
不需要前導/尾隨分號。
即使 VBA 正則表達式不支持后視,您也可以使用該限制。
(?:^|;)([^;]*?Co-Founder)
選項:區分大小寫(或不區分,如您所願); ^$ 匹配換行符
(?:^|;)
([^;]*?Co-Founder)
[^;]*?
Co-Founder
使用RegexBuddy創建
拆分整個字符串並結合正向過濾, getCoFounders()
function 將返回結果數組:
Sub ExampleCall()
Dim s As String
s = ";John Smith, Co-Founder;Jane Doe, CEO;James Jackson, Co-Founder;"
Debug.Print Join(getCoFounders(s), "|")
End Sub
Function getCoFounders(s As String)
getCoFounders = Filter(Split(s, ";"), "Co-Founder", True, vbTextCompare)
End Function
結果在 VB 編輯器的即時 window
John Smith, Co-Founder|James Jackson, Co-Founder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.