-5

I am using a constraint file for my system and basically i am using this line for parsing my values:

angle(Vector(JointA,jointB),Vector(JointA,jointB),minValue,maxValue)

Can you please help me and specify the regular expression of this line. I want to be able to retrieve the names of the four joints, including the minValue as well as the maxValue. Thanks!

Community
  • 1
  • 1
  • 3
    Over and over again people ask for regular expressions to parse context free languages. It's not even worth explaining anymore. Learn about the theory behind regular languages and expressions and you will find that can't be parsed (or at least it shouldn't be) by regex. Wikipedia has good articles on the theory relating to parsing. If you want a solution right now, just use methods in the string class, the solution you come up with will be just as versatile and pleasant to read as a regex to do it. – evanmcdonnal May 21 '13 at 15:38
  • This isn't a site where people give you code. This is where you come for help with code problems – Daniel May 21 '13 at 15:39
  • 1
    Daniel, it's a site where you can ask any algorithmic problem you have.. Some people are willing to help and some not. You shouldn't waste your time commenting. evanmcdonnal, thanks. I know that there are a lot of sources on this subject. But, the thing is that i have a limited time and it's difficult to find the solution. – user2406173 May 21 '13 at 15:46

2 Answers2

0

If you're just asking for a way to parse the variable bits from this kind of text, it's quite easy:

See here: http://tinyurl.com/qjdaj9w

var exp = new Regex(@"angle\(Vector\((.*?),(.*?)\),Vector\((.*?),(.*?)\),(.*?),(.*?)\)");

var match = exp.Match("angle(Vector(JointA,jointB),Vector(JointA,jointB),minValue,maxValue)");


var jointA1 = match.Groups[0];
var jointB1 = match.Groups[1];
var jointA2 = match.Groups[2];
var jointB2 = match.Groups[3];
var max     = match.Groups[4];
var min     = match.Groups[5];
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
0

Description

Providing your basic text is as simple as your sample it can be completed with this very basic regex which would capture your values.

angle[(]Vector[(]\b([^,]*)\b[,]\b([^)]*)\b[)][,]Vector[(]\b([^,]*)\b[,]\b([^)]*)\b[)]

  • group 0 will get the entire matched string
  • 1-4 will have the joint values requested

enter image description here

Disclaimer

Given that this is context free language using a regex is probably not the best solution as there are edge cases which will defeat the regex.

C# Code Example:

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          Regex re = new Regex(@"angle[(]Vector[(]\b([^,]*)\b[,]\b([^)]*)\b[)][,]Vector[(]\b([^,]*)\b[,]\b([^)]*)\b[)]",RegexOptions.IgnoreCase);
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}

$matches Array:
(
    [0] => Array
        (
            [0] => angle(Vector(JointA,jointB),Vector(JointA,jointB)
        )

    [1] => Array
        (
            [0] => JointA
        )

    [2] => Array
        (
            [0] => jointB
        )

    [3] => Array
        (
            [0] => JointA
        )

    [4] => Array
        (
            [0] => jointB
        )

)
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43