質問

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!

役に立ちましたか?

解決

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];

他のヒント

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
        )

)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top