簡體   English   中英

正則表達式匹配子字符串中的最后一次出現

[英]Regular expression to match the last occurrence in substring

我有這樣的文字

words words words tag a words words tag a words tag b words time words words tag c words time

我不知道a,b和c是什么。 我要捕獲的是“ tag b words time ”,“ tag c words time ”以及其他類似的詞,因此我嘗試的正則表達式為tag[\\w ]+?time ,但是與之匹配

tag a words words tag a words tag b words time

如何捕獲標簽的最后一次出現? 我正在使用matlab,因此如果有非正則表達式解決方案也很好。

應該這樣做:

tag\s*[\w]\s*[\w]+\s*time

您可以使用此正則表達式:

/tag [a-z]{4} [\w]+ time$/

這是一個工作示例(使用javascript,但是,如果您使用PCRE,則應在matlab中運行)。

 s1 = "words words words tag a words words tag a words tag b words time words words tag cccc words time" re = /tag [az]{4} [\\w]+ time$/ console.log(s1.match(re)) 

暫無
暫無

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

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