0

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
pRintLn
  • 139
  • 2
  • 8
  • 1
    This, to me at least, sounds a *little* bit like trying to parse HTML with regex - just about doable in **some** cases, but if you want something robust that copes with edge-cases you may need to write a state-based parser instead. – Marc Gravell Nov 30 '10 at 05:54
  • you're right, some funky regex would give me a quick solution to my problem whereas parser is the proper one... thanks – pRintLn Nov 30 '10 at 06:13
  • What you want may be possible, but frankly, I wouldn't put the time in answering your question because it lacks **a lot** of details. Is an indexer always simple? Can there be nested parentheses outside of strings? What about nested curly or square brackets? Are you really trying to parse C#, or is it some other language? And most importantly - what is your expected output? – Kobi Nov 30 '10 at 18:41

1 Answers1

1

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#

Community
  • 1
  • 1
Philip Rieck
  • 32,368
  • 11
  • 87
  • 99