0

I am reading a C# source file. When I encounter a string, I want to get it's value.
For instance, in the following example:

public class MyClass
{
  public MyClass()
  {
    string fileName = "C:\\Temp\\A Weird\"FileName";
  }
}  

I would like to retrieve

C:\Temp\A Weird"FileName

Is there an existing procedure to do that?
Coding a solution with all the possible cases should be quite tricky (@, escape sequences. ...).
I am convinced such procedure exists...
I would like to have the dual function too (to inject a string into a C# source file)

Thanks in advance.
Philippe

P.S:
I gave an example with a filename, but I look for a solution working for all kinds of strings.

8
  • so you want all string variables? Commented Sep 12, 2013 at 10:20
  • @"C:\Temp\A Weird FileName"; and than insert the " between Weird FileName. Commented Sep 12, 2013 at 10:20
  • 1
    Are you looking for Path.GetFullPath(fileName)? Commented Sep 12, 2013 at 10:22
  • What is it you want to achieve? A typical approach in language dependent strings is handling them separately. In your code that would be string fileName = MyStringBundle["TheWeirdFileNameString"], and then would StringBundle have all its strings stored with IDs in dedicated string files. Commented Sep 12, 2013 at 10:22
  • 1
    You just need a regex that will get you everything between " (inverted commas), unless its the one preceeded by backslash, like in your example Commented Sep 12, 2013 at 10:22

3 Answers 3

2

I'm pretty sure you can use CodeDOM to read a C# code file and parse its elements. It generates a code tree, and then you can look for nodes representing strings.

http://www.codeproject.com/Articles/2502/C-CodeDOM-parser

Other CodeDom parsers:

http://www.codeproject.com/Articles/14383/An-Expression-Parser-for-CodeDom

NRefactory: https://github.com/icsharpcode/NRefactory and http://www.codeproject.com/Articles/408663/Using-NRefactory-for-analyzing-Csharp-code

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

Comments

1

There is a way of extracting these strings using a regular expression:

  ("(\\"|[^"])*")

This particular one works on your simple example and gives the filename (complete with leading and trailing quote characters); whether it would work on more complex ones I can't easily tell unfortunately.

For clarity, (\\"|[^"]) matches any character apart from ", except where it has a leading \ character.

Comments

0

Just use ".*" Regex to match all string values, then remove trailing inverted commas and unescape it.

this will allow \" and "" characters inside your string

so both "C:\\Temp\\A Weird\"FileName" and "Hello ""World""" will match

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.