我有這樣的文字,其他文本里面:reqex匹配字符串
[text_box title="Michael Jordan" align="left"] Basketballplayer ...
[/text_box]
我有這段文字與reqex匹配,並且將其替換爲:
<div class="text-box alignleft"> <h4>Michael Jordan</h4>
哪能文本框匹配嗎?文本框可以在我的文本中出現不止一次,具有不同的標題和對齊方式。
謝謝
我有這樣的文字,其他文本里面:reqex匹配字符串
[text_box title="Michael Jordan" align="left"] Basketballplayer ...
[/text_box]
我有這段文字與reqex匹配,並且將其替換爲:
<div class="text-box alignleft"> <h4>Michael Jordan</h4>
哪能文本框匹配嗎?文本框可以在我的文本中出現不止一次,具有不同的標題和對齊方式。
謝謝
可以使用following regex如果你的文本是荃結構是這樣的:2組
\[text_box title="(.*?)" align="(.*?)"
一個集團將成爲標題內容和內容
Regex rgx = new Regex("\\[text_box title=\"(.*?)\" align=\"(.*?)\"");
MatchCollection matches = rgx.Matches(input);
foreach(var match in matches)
{
string title = match.Groups[1].Value;
string alignment = match.Groups[2].Value;
string output = string.Format("<div class=\"text-box align{1}"> <h4>{0}</h4>", title, alignment);
// or in newer .Net Framework
string output = $"<div class=\"text-box align{alignment}"> <h4>{title}</h4>";
}
這不會產生任何輸出。解析模板只有1/4的工作。 –
也許如果提供任何代碼,我可以幫助指出正確的方向。但沒有。基於正則表達式,操作可以自己嘗試一些東西,並回到一組更具體的問題。畢竟這不是諮詢網站 –
例如,風格從哪裏來?正則表達式應該識別「text_box」並將其映射到「文本框」?那麼*替換*呢? –
嘗試使用以下代碼片段:
string input = "[text_box title=""Michael Jordan"" align=""left""] Basketballplayer[/text_box]";
string pattern = "\[text_box title=""([^""]+)"" align=""([^""]+)"".*";
string replacement = "<div class=""text-box align$2""> <h4>$1</h4></div>";
string result = Regex.Replace(input, pattern, replacement);
要了解模板和替換,您可以看到MSDN。它可以幫助你開始。
我更喜歡Expresso工具來編寫和測試正則表達式。它也會生成C#代碼,請參閱使用expresso生成的以下代碼塊。
using System.Text.RegularExpressions;
/// <summary>
/// Regular expression built for C# on: Mon, May 29, 2017, 02:24:58 PM
/// Using Expresso Version: 3.0.5854, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// \[text_box title="
/// Literal [
/// text_box
/// Space
/// title="
/// [title]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// " align="
/// "
/// Space
/// align="
/// [align]: A named capture group. [.*?]
/// Any character, any number of repetitions, as few as possible
/// ".*\]
/// "
/// Any character, any number of repetitions
/// Literal ]
/// [data]: A named capture group. [.*]
/// Any character, any number of repetitions
/// \[\/text_box\]
/// Literal [
/// Literal/
/// text_box
/// Literal ]
///
///
/// </summary>
public static Regex regex = new Regex(
"\\[text_box title=\"(?<title>.*?)\" align=\"(?<align>.*?)\"."+
"*\\](?<data>.*)\\[\\/text_box\\]",
RegexOptions.Singleline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
// This is the replacement string
public static string regexReplace =
"<div class=\"text-box align${align}\"><h4>${title}</h4>";
//// Replace the matched text in the InputText using the replacement pattern
string result = regex.Replace(InputText,regexReplace);
//// Split the InputText wherever the regex matches
string[] results = regex.Split(InputText);
//// Capture the first Match, if any, in the InputText
Match m = regex.Match(InputText);
//// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(InputText);
//// Test to see if there is a match in the InputText
bool IsMatch = regex.IsMatch(InputText);
//// Get the names of all the named and numbered capture groups
string[] GroupNames = regex.GetGroupNames();
//// Get the numbers of all the named and numbered capture groups
int[] GroupNumbers = regex.GetGroupNumbers();
你有沒有試過自己的東西? –
這不是一個搜索或替換。你有一個你想填寫的模板。正則表達式不是*最好的方法。 –
將該僞代碼解釋爲文本並在其上使用正則表達式在實踐中幾乎肯定是一個壞主意。你可能需要像某種解析器那樣更強大一些。 – Neil