我有这个文本,在另一个文本中:
[text_box title="Michael Jordan" align="left"] Basketballplayer ...
[/text_box]
我必须将此文本与reqex匹配,并将其替换为:
<div class="text-box alignleft"> <h4>Michael Jordan</h4>
我如何匹配文本框?文本框可以在我的文本中出现多次,具有不同的标题和对齐方式。
谢谢
答案 0 :(得分:0)
如果您的文本框的结构如下所示,则可以使用following regex:
\[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 :(得分:0)
尝试使用以下代码段:
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。它可以帮助您开始。
答案 2 :(得分:0)
我更喜欢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();