开发者

Find a string in a string, using context (Scripting)

开发者 https://www.devze.com 2023-03-22 02:08 出处:网络
I\'m working on a simply 2D game engine with a simple Scripting Language. Here is a example how the script should look like:

I'm working on a simply 2D game engine with a simple Scripting Language. Here is a example how the script should look like:

<a=AssetName>
<scripthere>
<script
<dialogue
contiu开发者_Python百科e dialogue
<a2=AlternativeAssetName
<script>

The problem, I have, is how to get the 'AssetName' out of the first line (which must not be the first line). What is the best way to do this? Regular Expressions (never used them, and Google had no help on how I could use them)?

I want to allow it in the language to end a command either with '>' or with the start of a new command '<'. So the string would be defined something like this:

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

Thank you in advance. :)


I would pay attention to the comments above first :)
But, if still you want to go your way, here's how (in 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();

Play with this code in the debugger, so you can make it fit your needs.
If needed, read more here or here about regular expressions.
Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号