簡體   English   中英

我需要一個正則表達式來檢測MailChimp標簽

[英]I need a regular expression to detect MailChimp tags

我有一個html字符串,我需要通過正則表達式。 此表達式必須以以下格式檢測MailChimp標記: *|TagName|*

我對正則表達式不太熟悉。 我目前使用的是: \\*\\|([^]]*)\\|\\*

但是他的正則表達式檢測到2個匹配之間的while字符串。 所以如果我們有:

"Sample text with user *|User_Name|*, to verify regular expression with *|Date_Value|*, and some other text"

比賽將是:

 "*|User_Name|*, to verify regular expression with *|Date_Value|*"

如果有人可以告訴我如何更改表達式或使用什么來代替分別檢測所有匹配項。

謝謝

請試試這個正則表達式:

  string source = 
    "Sample text with user *|User_Name|*, to verify regular expression with *|Date_Value|*, and some other text";

  string pattern = @"\*\|.*?\|\*"; // please, notice ".*?" instead of ".*"

  // ["*|User_Name|*", "*|Date_Value|*"]
  string[] matches = Regex
    .Matches(source, pattern)
    .OfType<Match>()
    .Select(match => match.Value)
    .ToArray();

訣竅在於.*? 而不是.* - 匹配盡可能少的字母

暫無
暫無

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

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