开发者

.Net C# regex for parsing method/property chain calls

I’m trying to parse string like:

.A.B[C].D(E).F("(G) / {H})", "1" == "1").I.J[K]

where A,B,I,J are property names, D,F method names, E, "(G) / {H})", "1" == "1" method parameters and C and K index values. . The parameters can contain any characters and the string may 开发者_如何转开发contain any number of properties and/or methods.

I’m looking for a regex that would do that job. So far I came up with

(?<=\.)(?< PropertyOrMethodName>\w+)((\\[(?< Index>\w+)\\])|((?< Open>\\()(?< Parameters>.+)(?<-Open>\\))(?(Open)(?!))))?

but it’s not good e.g. for the above sample captures D(E).F("(G) / {H})", "1" == "1") together.


I don't think you will get good results at all with a regex, unless you have a severely limited sample of c# you wish to parse - and if that's the case, you'd be better off giving a sample of the possible lines in your question.

For example:

  public void SomeMethod(int x)
  {
     foo.bar.baz(x, y => qux(y))[(x - 1)] = e.value;
  }

Even if you come up with a regex that can successfully parse this and other complex cases (good luck) - is foo a namespace, bar a class, and baz a static method? or is foo a field on the current type, bar a property on that, and baz an instance method? Without a parser, you really don't get any insight at all to what each element means.

I would suggest looking at a parser - try starting with this SO question: Parser for C#

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜