如何在两个标记内找到字符串

时间:2013-04-09 01:42:11

标签: python regex

我正在使用python进行文本处理。基本上我想在两个地标之间提取内容。具体内容如下。如何设计正则表达式以获取“查找意图漏洞”和“打印意图摘要”之间的文本。感谢

    Find component vulnerabilities
    ******************************************************************************************
    !!!@methods_for_class[org.apache.cordova.BatteryListener$1*org/apache/cordova/BatteryListener/execute(Ljava/lang/String;Lorg/json/JSONArray;Lorg/apache/cordova/api/CallbackContext;)@70!] is nil
    !!!@methods_for_class[org.apache.cordova.CordovaWebView$1*org/apache/cordova/CordovaWebView/setup()@124!] is nil
    !!!@methods_for_class[org.apache.cordova.NetworkManager$1*org/apache/cordova/NetworkManager/initialize(Lorg/apache/cordova/api/CordovaInterface;Lorg/apache/cordova/CordovaWebView;)@57!] is nil
    !!!@methods_for_class[org.apache.cordova.Device$1*org/apache/cordova/Device/initTelephonyReceiver()@29!] is nil
    Protected Receiver: org.apache.cordova.BatteryListener$1*org/apache/cordova/BatteryListener/execute(Ljava/lang/String;Lorg/json/JSONArray;Lorg/apache/cordova/api/CallbackContext;)@70!, 0
    Protected Receiver: org.apache.cordova.CordovaWebView$1*org/apache/cordova/CordovaWebView/setup()@124!, 0
    Possible Malicious Broadcast Injection: org.apache.cordova.NetworkManager$1*org/apache/cordova/NetworkManager/initialize(Lorg/apache/cordova/api/CordovaInterface;Lorg/apache/cordova/CordovaWebView;)@57, 0
    Possible Malicious Broadcast Injection: org.apache.cordova.Device$1*org/apache/cordova/Device/initTelephonyReceiver()@29, 0

    Find intent vulnerabilities
    ******************************************************************************************
    Possible Activity Hijacking: org/apache/cordova/CordovaWebView/showWebPage(Ljava/lang/String;ZZLjava/util/HashMap;)@147, Source Line: 664, hasExtras=false, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/CordovaWebView/showWebPage(Ljava/lang/String;ZZLjava/util/HashMap;)@201, Source Line: 676, hasExtras=false, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/CordovaWebViewClient/shouldOverrideUrlLoading(Landroid/webkit/WebView;Ljava/lang/String;)@83, Source Line: 131, hasExtras=false, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/CordovaWebViewClient/shouldOverrideUrlLoading(Landroid/webkit/WebView;Ljava/lang/String;)@161, Source Line: 142, hasExtras=false, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/CordovaWebViewClient/shouldOverrideUrlLoading(Landroid/webkit/WebView;Ljava/lang/String;)@239, Source Line: 153, hasExtras=false, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/CordovaWebViewClient/shouldOverrideUrlLoading(Landroid/webkit/WebView;Ljava/lang/String;)@368, Source Line: 185, hasExtras=true, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/CordovaWebViewClient/shouldOverrideUrlLoading(Landroid/webkit/WebView;Ljava/lang/String;)@544, Source Line: 209, hasExtras=false, hasRead=false, hasWrite=false
    Possible Service Hijacking: org/apache/cordova/api/LegacyContext/bindService(Landroid/content/Intent;Landroid/content/ServiceConnection;I)@22, Source Line: 142, hasExtras=false, hasRead=false, hasWrite=false
    Possible Activity Hijacking: org/apache/cordova/api/LegacyContext/startActivity(Landroid/content/Intent;)@20, Source Line: 82, hasExtras=false, hasRead=false, hasWrite=false
    Possible Service Hijacking: org/apache/cordova/api/LegacyContext/startService(Landroid/content/Intent;)@20, Source Line: 136, hasExtras=false, hasRead=false, hasWrite=false

    Print intent summary
    ******************************************************************************************
    **************************
    org/apache/cordova/Capture/captureAudio()@8
    invoke-direct   {v0,v1},android/content/Intent/<init>   ; <init>(Ljava/lang/String;)V
    Explicit: false
    Destination Type: 
    Done: false
    **************************

2 个答案:

答案 0 :(得分:2)

你需要使用正则表达式吗?使用str.find或str.index查找每个标记然后使用切片来提取它们之间的内容可能更简单。

但是,使用正则表达式:

re.search(r"(?s)Find intent vulnerabilities\n(.*?)Print intent summary\n", text).group(1)

答案 1 :(得分:2)

正则表达式可能在这里有点过分。为什么不使用索引和切片?

>>> l = len('test123')
>>> s = '  test123 somestuff here test456   '
>>> s.index('test123')
2
>>> s.index('test456')
25
>>> s[2+l:25]
' somestuff here '