2017-04-16 87 views
0

例如,我想解析python文件與三重雙引號之間的文本,並從該文本生成html表。例如像正則表達式雙引號和換行符之間的提取

""" 
Replaces greater than operator ('>') with 'NOT BETWEEN 0 AND #' 
Replaces equals operator ('=') with 'BETWEEN # AND #' 

Tested against: 
    * Microsoft SQL Server 2005 
    * MySQL 4, 5.0 and 5.5 
    * Oracle 10g 
    * PostgreSQL 8.3, 8.4, 9.0 

Requirement: 
    * Microsoft Access 

Notes: 
    * Useful to bypass weak and bespoke web application firewalls that 
     filter the greater than character 
    * The BETWEEN clause is SQL standard. Hence, this tamper script 
     should work against all (?) databases 

>>> tamper('1 AND A > B--') 
'1 AND A NOT BETWEEN 0 AND B--' 
>>> tamper('1 AND A = B--') 
'1 AND A BETWEEN B AND B--' 
""" 

HTML表格必須是簡單的表包含5列

  1. 柱一切"""\n if new line is empty之間
  2. 柱一切Tested against:\n if new line is emptyRequirement:之間和

    文本塊\n if new line is empty

  3. 柱一切Notes:\n if new line is empty
  4. 柱之間的所有>>>\n
  5. 之間柱一切

4 column end\n之間,結果必然是:

  1. 替換大於運算符( '>' )'NOT BETWEEN 0 AND#' 用'BETWEEN#AND#'替換等於運算符('=')
    • 的Microsoft SQL Server 2005

      • 的MySQL 4,5.0和5.5
      • 的Oracle 10g
      • 的PostgreSQL 8.3,8.4,9.0

      • Microsoft Access
    • 有用繞過弱和定製的web應用程序的防火牆 過濾除字符
    • 之間子句越大SQL標準。因此,這種篡改腳本 應反對各種(?)數據庫
  2. 篡改( '1個A> B--') 篡改( '1個A = B--')

  3. 「1和未介於0和B--」 「1和B之間和B--」

我可以使用什麼樣的語法來提取? 我將使用VBScript.RegExp。

Set fso = CreateObject("Scripting.FileSystemObject") 
txt = fso.OpenTextFile("C:\path\to\your.py").ReadAll 

Set re = New RegExp 
re.Pattern = """([^""]*)""" 
re.Global = True 

For Each m In re.Execute(txt) 
    WScript.Echo m.SubMatches(0) 
Next 

回答

2

你的問題是相當廣泛的,所以我只是概述一種處理這個問題的方法。否則,我不得不爲你編寫整個腳本,這是不會發生的。

  1. 提取docquotes之間的所有內容。使用正則表達式這樣提取docquotes之間的文本:

    Set re1 = New RegExp 
    re1.Pattern = """""""([\s\S]*?)""""""" 
    
    For Each m In re1.Execute(txt) 
        docstr = m.SubMatches(0) 
    Next 
    

    請注意,您需要設置re.GlobalTrue,如果你在你的文件超過1個文檔字符串,並希望所有的人處理。否則,你只會得到第一場比賽。

  2. 刪除前導和與第二正則表達式結尾的空白:

    Set re2 = New RegExp 
    re2.Pattern = "^\s*|\s*$" 
    re2.Global = True 'find all matches 
    
    docstr = re2.Replace(docstr, "") 
    

    不能使用Trim對於這一點,因爲函數只處理空間,而不是其他的空白。

  3. 無論是在分割連續2+行字符串分解得到的文檔部分,或使用其他正則表達式來提取它們:

    Set re3 = New RegExp 
    re3.Pattern = "([\s\S]*?)\r\n\r\n" + 
           "Tested against:\r\n([\s\S]*?)\r\n\r\n" + 
           ... 
    
    For Each m In re3.Execute(txt) 
        descr = m.SubMatches(0) 
        tested = m.SubMatches(1) 
        ... 
    Next 
    

繼續打破章節,直到你具備的要素你想顯示。然後從這些元素構建HTML。

+0

非常感謝你 –

+0

優秀的解釋。 – Lankymart

相關問題