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.

3
  • 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. Commented Nov 30, 2010 at 5:54
  • you're right, some funky regex would give me a quick solution to my problem whereas parser is the proper one... thanks Commented Nov 30, 2010 at 6: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? Commented Nov 30, 2010 at 18:41

1 Answer 1

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#

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.