【问题标题】:Find a string in a string, using context (Scripting)在字符串中查找字符串,使用上下文(脚本)
【发布时间】:2011-10-11 03:40:19
【问题描述】:

我正在使用简单的脚本语言开发一个简单的 2D 游戏引擎。 下面是脚本的示例:

<a=AssetName>
<scripthere>
<script
<dialogue
contiue dialogue
<a2=AlternativeAssetName
<script>

我遇到的问题是如何将“资产名称”从第一行(不能是第一行)中取出。做这个的最好方式是什么?正则表达式(从未使用过,Google 对我如何使用它们没有帮助)?

我想允许它在语言中以“>”或新命令“

"<a=" + (string)AssetName + (">" || "<")

提前谢谢你。 :)

【问题讨论】:

  • 我建议为此使用标准格式,这样您就不必编写自己的所有内容(json、xml、yaml、ini 等)
  • 您为什么要发明一种新的 XML 语言?我严重怀疑您在正则表达式上找不到任何帮助。我知道一个事实,当我 6 个月前做一些研究时,我发现了 RegexBuddy。

标签: c# regex scripting


【解决方案1】:

我会先关注上面的 cmets :)
但是,如果您仍然想按自己的方式行事,请按照以下方式(在 C# 中):

string pattern = @"\< *a\=([^\<\>]+) *[\<\>]";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = r.Match(tosearch);
if ( m.Success )
{
  foreach (Group g in m.Groups)
  {
    Console.WriteLine("Group: " + g.Value);
  }
}
else
{
  Console.WriteLine("No matches");
}
Console.WriteLine();

//To keep the console window open after running the application, add the following lines of code:
System.Console.WriteLine("Press any key to Continue...");
System.Console.ReadLine();

在调试器中使用此代码,以使其满足您的需求。
如果需要,请阅读更多关于正则表达式的 herehere
希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多