Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Regex in C# is seemingly not supported by SE

Discussion in 'Programming Questions and Suggestions' started by sioxernic, Jan 3, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. sioxernic Senior Engineer

    Messages:
    2,535
    I'm still quite new to C# personally, but I attempted to use C#'s internal Regex functionality through the ingame editor but apparently it is one of the things we are locked out from.

    Can anyone else confirm this?

    Does anyone know of another good way of grabbing specific portions of text from a string akin to regex (Doesn't need to be as advanced)?
     
  2. Cuber Apprentice Engineer

    Messages:
    262
    Looking at the Modding API's whitelist, it would seem like System.Text.RegularExpressions.Regex should be accessible!

     
  3. WDMeaun Apprentice Engineer

    Messages:
    142
    A bit of a necro here, but I found this post first during a search, as I'm relatively new to both Space Engineers and C#.

    It indeed is enabled, but you need to use the entire namespace.
    The code posted at the end works perfectly and mostly has copy paste code from msdn.
    It's unfinished, so it just returns 0.0F, but the debug Beacon shows a correct value.

    Can anyone tell me how significant the gain/loss of performance is with regular expressions versus normal string comparison methods?
    I am in love with regular expressions, so the answer probably wont make a big difference ;)

    Also wondering how much difference these long variables names make versus compiled-for-performance kinda code. Can google both subjects, but I try to focus more on practical output at the moment.
    (as in.. instead of PatternPistonDistance -> _p or something)


    Code:
    string PatternPistonDistance = "Current position: ([0-9.]+)m";
    System.Text.RegularExpressions.Regex RegexPistonDistance = null;
    
    
    public float GetPistonDistance(IMyPistonBase piston) {
        string output = "";
        float ret = 0.0F;
        if (RegexPistonDistance == null) {
            RegexPistonDistance = new System.Text.RegularExpressions.Regex(PatternPistonDistance);
        }
        System.Text.RegularExpressions.Match m = RegexPistonDistance.Match(piston.DetailedInfo);
        if (m.Success && m.Groups.Count == 2) {
            System.Text.RegularExpressions.Group g = m.Groups[1];
            System.Text.RegularExpressions.CaptureCollection cc = g.Captures;
            for (int j = 0; j < cc.Count; j++) 
            {
               System.Text.RegularExpressions.Capture c = cc[j];
               output += " " + "Capture"+j+"='" + c + "', Position="+c.Index;
            }
        }
        Beacon.SetCustomName(output);
        return ret;
    }
    
     
    Last edited by a moderator: Jan 14, 2015
Thread Status:
This last post in this thread was made more than 31 days old.