Saber
Saber

Reputation: 133

Splitting a special string to some parts

I have a string as

event FILE[2]::OnProc {;? Msg("Step2") } event FILE[test]::OnProc {;? Msg("Step3.1.1") }

I would like to split into some parts. the desired output wold be a list as

List
    [0]
       2
       ;? Msg("Step2")
    [1]
       test
       ;? Msg("Step3.1.1") 

As you see the string between [] is the first entry of ith entry of list and the string between {} is the next entry.

May I ask your help how can I have the desired list?

Update:

I have tried this code but it is not going to be correct.

Regex regex = new Regex(@"event FILE\[(?<type>[^\]]*)\]::OnProc? ?{;(?<msg>[^}]*)}");

Upvotes: 2

Views: 62

Answers (3)

Saber
Saber

Reputation: 133

Thanks to all answers. I think what I have here depending on Regex will be a nice one.

Regex regex = new Regex(@"event ACTION\[(?<type>[^\]]*)\]::OnProc {? ?;(?<msg>[^}]*)}");
List<ScriptData> Script = new List<ScriptData>();
string ScriptTxt1 = List.ScriptComp[0].CompScrTxt;

//convert first script
foreach (Match match in regex.Matches(ScriptTxt1))
{
    Script.Add(new ScriptData(match.Groups["type"].Value, match.Groups["msg"].Value));
}

public class ScriptData
{
    public string Type { get; private set; }
    public string ScriptMessage { get; private set; }

    public ActionScriptData(string type, string msg)
    {
        Type = type;
        ScriptMessage = msg;
    }

    public ScriptData()
    {
        Type = null;
        ScriptMessage = null;
    }
}

Upvotes: 0

M. Schena
M. Schena

Reputation: 2107

You can use Split.Extract (approach without Regex):

var list = new List<KeyValuePair<string, string>>();
string str = "event FILE[2]::OnProc {;? Msg(\"Step2\") } event FILE[test]::OnProc {;? Msg(\"Step3.1.1\") }";

List<string> events = Split.Extract(str, "event", "event");
foreach (string e in events)
{
    list.Add(new KeyValuePair<string, string>(
        Split.Extract(e, "FILE[", "]").First(), 
        Split.Extract(e, "{", "}").First()
    ));
}

And the additional Split Class:

public class Split
{
    public static List<string> Extract(string source, string splitStart, string splitEnd)
    {
        try
        {
            var results = new List<string>();

            string[] start = new string[] { splitStart };
            string[] end = new string[] { splitEnd };
            string[] temp = source.Split(start, StringSplitOptions.None);

            for (int i = 1; i < temp.Length; i++)
            {
                results.Add(temp[i].Split(end, StringSplitOptions.None)[0]);
            }

            return results;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }
}

(I don't like complicated Regex :P)

Upvotes: 1

Ben
Ben

Reputation: 26

string toSplit = "event FILE[2]::OnProc {;? Msg("Step2") } event FILE[test]::OnProc {;? Msg("Step3.1.1") }";
string [] splitString = toSplit.split(new string [] {"event FILE"}, String.SplitOptions.None);

    List<string> parts = new List();

    foreach(string s in splitString )
    {
      parts.Add({reg ex for []); //psuedocoded because I don't like to figure out regex
      parts.Add({reg ex for {}); //psuedocoded because I don't like to figure out regex
    }

I am sure someone else can fill in the RegEx that loves them!

Upvotes: 0

Related Questions