【发布时间】:2016-05-06 19:05:30
【问题描述】:
我正在尝试获取一个正则表达式,我可以插入该表达式以查找文件中的所有字符串。例如,如果我有一个文件有
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 希望表达式返回 ["Value", "temp"]
这是我现在正在使用的 python 文件。
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
【问题讨论】:
-
这是 C# 吗?然后,更可靠的方法是使用 AST 解析器,请参阅stackoverflow.com/questions/1432998/…。
-
@SIslam
Value和temp- OP 正在寻找字符串。