0

Im trying to get a regex expression that i can plugin to find all strings in a file. For example if i had a file that had

using System;

public class Test: TestClass{
    public int val1 = 0;
    public string val2 = "Value";
    //This is a "test" class
    public Test(value = "temp"){
        val2 = value;
    }
}

id want the expression to return ["Value", "temp"]

here is my python file im using now.

import os
import shutil
import subprocess
import codecs
import sys
import re
pattern = re.compile("((?:[^\"\\\\]|\\\\.)*)")
with codecs.open(filepath,"r","utf-8") as f:
    for line in f.readlines():
       m = re.findall(pattern, line)
       for string in m:
           print string
2
  • 1
    Is this C#? Then, a more reliable approach would be to use the AST parser, see stackoverflow.com/questions/1432998/…. Commented Jan 29, 2016 at 6:02
  • @SIslam Value and temp - the OP is looking for strings. Commented Jan 29, 2016 at 6:04

1 Answer 1

4

Apply re.findall function on the lines which won't startswith //.

with codecs.open(filepath,"r","utf-8") as f:
    out = []
    for line in f:
        if not line.strip().startswith('//'):
            out.extend(re.findall(r'"([^"]*)"', line))
    print out
Sign up to request clarification or add additional context in comments.

3 Comments

@WilliamMcCarty Do you need it to work for /* */ comments as well?
@AvinashRaj That's my point. If he wants it to work for all C#-like files, your solution won't always work.
@pushkin yes that would help as well, but from my question originally the answer given works.

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.