2016-06-14 61 views
0

我正在循环使用大量的.PHP文件,将它们视为纯文本,并试图找到某些函数的文本参数。在文本中查找特定参数

PHP文件中的函数全部通过使用\L10n::来调用,然后获取字符串参数。

我试图找到的文本示例如下。

我已经运用以下正则表达式正确找到了这个问题。

pattern = re.compile("L10n::[\w]+\((?:\'(.*?)\')\,?\s?(?:\'(.*?)\')*", re.MULTILINE | re.IGNORECASE | re.DOTALL) 

OR

pattern = re.compile("\\L10n::(.*?)\('(.*?)'\)", re.MULTILINE | re.IGNORECASE | re.DOTALL) 


bar\L10n::__('Double _')baz 
bar\L10n::esc_attr__('Escape Attributes __')baz 
bar\L10n::esc_html__('Escapted HTML __')baz 
bar\L10n::_e('Echos')baz 
bar\L10n::esc_html_e('Echo Escaped HTML')baz 
bar\L10n::_x('Underscore X')baz 
bar\L10n::_ex('Echo underscore x')baz 
bar\L10n::esc_attr_x('Escape Attribute X')baz 
bar\L10n::esc_html_x('Escaped HTML X')baz 
bar\L10n::_n('Nothing')baz 
bar\L10n::_nx('No X')baz 
bar\L10n::_n_noop('N No-Op')baz 
bar\L10n::_nx_noop('No X No-Op')baz 

有了这样说,一些多个参数

bar\L10n::_n('Text 1', 'Text 2', $variable) 

在这种情况下,我想文本1和文本2,而不是$变量。

为了让它更有趣...有时参数不是全部在一行上。

bar\L10n::_n(
    'Text 1', 
    'Text 2', 
    $variable 
) 

如果文本有逃脱“里面,如‘这看起来在人们间没有\’这里T”

第二个正则表达式拍打我上面有我上面有了第一个正则表达式模式失败如果有多个文本变量,则会失败。 (它也带来了_n部分,但没关系)

任何帮助,将不胜感激。

编辑:

我也应该状态,会出现在文件中的其他功能,我希望忽略。

如:

foo\file::__('function to ignore') 

我不想以配合这些。

我也想匹配L10n函数在其他函数中用作参数的地方。

EG

bar\file::__(bar\L10n::_e('Text 1'), 'Other variable to ignore') 

回答

0

让我试试我这一个:

import re 
input = """bar\L10n::__('Double _')baz 
bar\L10n::esc_attr__('Escape Attributes __')baz 
bar\L10n::_n(
    'Text 1', 
    'Text 2', 
    $variable 
)""" 
input = input.replace('\n', '') 
reg = re.compile('\'(.*?)\'') 
foo = reg.findall(input) 

给出了一个数组:

['Double _', 'Escape Attributes __', 'Text 1', 'Text 2'] 

现在,如果你想获得幻想和索引一切在一个散列,以便您可以轻松地迭代一切?

import re 
input = """bar\L10n::__('Double _', 'another')baz 
bar\L10n::esc_attr__('Escape Attributes __')baz 
bar\L10n::_n(
    'Text 1', 
    'Text 2', 
    $variable 
)""" 
dict = {} 
input = input.split('bar\\L10n::') 
regName = re.compile('(.*)\(') 
regAttr = re.compile('\'(.*?)\'') 
for i in input: 
    foo = regName.search(i) 
    if foo is not None: 
     dict[foo.group(1)] = regAttr.findall(i) 

将使字典,看起来像:

{'__': ['Double _', 'another'], '_n': ['Text 1', 'Text 2'], 'esc_attr__': ['Escape Attributes __']} 

希望这有助于!

有一个很好的网站叫做Rubular,我用它作为正则表达式,它应该是用于Ruby,但我用它来做Ruby,Python和Perl正则表达式。我建议检查一下,如果你认为你会做更多的正则表达式的东西。

编辑:(注释中讨论后)

import re 

globalDict = {} 

for file in directory: # pseudo-code, implement this loop yourself 

    fileContents = """bar\L10n::__('Double _', 'another')baz 
    bar\L10n::esc_attr__('Escape Attributes __')baz 
    bar\L10n::_n(
     'Text 1', 
     'Text 2', 
     $variable 
    )""" 
    regAttr = re.compile('\'(.*?)\'') 
    for i in regAttr.findall(fileContents): 
     if i in globalDict.keys(): 
      globalDict[i].append('filename') 
     else: 
      globalDict[i] = ['filename'] 
+0

感谢您的。 我想要一本字典,所以这很容易,但我希望它的结构如下: '{'Double _':['filename_1','filename_2'],'Text 1': ['filename_2','filename_3'] ..} (之前我得到这个代码我循环通过文件,所以我想创建一个文件的列表中找到该短语) 你会怎么样改变你的代码来适应这个? –

+0

使用regAttr正则表达式并将它们用作键可能?对不起,我不熟悉.PHP文件。 –

+0

查看我的其他评论。 没关系......被搜索有点可怕! :( –

相关问题