Diese funktioniert für mich:
>>> haystack = """--c5eda821-H-
Message: Warning. Match of "eq 1" against "&ARGS:CSRF_TOKEN" required. [file "/usr/share/modsecurity-crs/optional_rules/modsecurity_crs_43_csrf_protection.conf"] [line "31"] [id "981143"] [msg "CSRF Attack Detected - Missing CSRF Token."]
Message: Warning. Pattern match "(.*?)=(?i)(?!.*httponly.*)(.*$)" at RESPONSE_HEADERS:Set-Cookie. [file "/usr/share/modsecurity-crs/optional_rules/modsecurity_crs_55_application_defects.conf"] [line "83"] [id "981184"] [msg "AppDefect: Missing HttpOnly Cookie Flag for auth."] [tag "WASCTC/WASC-15"] [tag "MISCONFIGURATION"] [tag "http://websecuritytool.codeplex.com/wikipage?title=Checks#cookie-not-setting-httponly-flag"]
Apache-Handler: application/x-httpd-php
Stopwatch: 1501247328778702 7722 (- - -)
Stopwatch2: 1501247328778702 7722; combined=2901, p1=886, p2=1609, p3=54, p4=87, p5=213, sr=309, sw=52, l=0, gc=0
Response-Body-Transformed: Dechunked
Producer: ModSecurity for Apache/2.7.7 (http://www.modsecurity.org/);
OWASP_CRS/2.2.8.
Server: Apache
WebApp-Info: "default" "59EFAF5D261B7D5BE14460C1BF3EE0A9" ""
Engine-Mode: "DETECTION_ONLY"
--c5eda821-Z--"""
>>> print(re.search(r'--[\da-e]{8}-\w-(.+?)--[\da-e]{8}-\w--$', haystack, re.M|re.DOTALL).group(1))
Message: Warning. Match of "eq 1" against "&ARGS:CSRF_TOKEN" required. [file "/usr/share/modsecurity-crs/optional_rules/modsecurity_crs_43_csrf_protection.conf"] [line "31"] [id "981143"] [msg "CSRF Attack Detected - Missing CSRF Token."]
Message: Warning. Pattern match "(.*?)=(?i)(?!.*httponly.*)(.*$)" at RESPONSE_HEADERS:Set-Cookie. [file "/usr/share/modsecurity-crs/optional_rules/modsecurity_crs_55_application_defects.conf"] [line "83"] [id "981184"] [msg "AppDefect: Missing HttpOnly Cookie Flag for auth."] [tag "WASCTC/WASC-15"] [tag "MISCONFIGURATION"] [tag
"http://websecuritytool.codeplex.com/wikipage?title=Checks#cookie-not-setting-httponly-flag"]
Apache-Handler: application/x-httpd-php
Stopwatch: 1501247328778702 7722 (- - -)
Stopwatch2: 1501247328778702 7722; combined=2901, p1=886, p2=1609, p3=54, p4=87, p5=213, sr=309, sw=52, l=0, gc=0
Response-Body-Transformed: Dechunked
Producer: ModSecurity for Apache/2.7.7 (http://www.modsecurity.org/);
OWASP_CRS/2.2.8.
Server: Apache
WebApp-Info: "default" "59EFAF5D261B7D5BE14460C1BF3EE0A9" ""
Engine-Mode: "DETECTION_ONLY"
Die Fehlermeldung, die Sie beschreiben, ist, weil, wenn es keine Übereinstimmung gibt, re.search
retur ns None
und None
hat kein groups
Attribut.
Ich bestelle diese Ausnahme zu verhindern, sollten Sie den Rückgabewert der Methode testen, um zu überprüfen, ob es eine Übereinstimmung gibt:
regex = re.compile(r'--[\da-e]{8}-\w-(.+?)--[\da-e]{8}-\w--$', re.M|re.DOTALL)
match = regex.search(haystack)
if match:
print match.group(1)
else:
print "No match"
[UPDATE]
Yeah you are right in case of one only long string, I have many more in the same fashion. But, i only want the content between those tags. – dumbo
Try this:
>>> regex = re.compile(r'--[\da-e]{8}-\w--(.+?)--[\da-e]{8}-\w--', re.M|re.DOTALL)
>>> for i, match in enumerate(regex.findall(haystack)):
... print('{:02d}-> {}...'.format(i, match[:15].strip()))
00-> HTTP/1.1 200 O...
01-> Message: Warni...
Die findall
Methode wird eine Liste der Spiele zurück. Wenn Sie nur das letzte Spiel wollen:
>>> matches = regex.findall(haystack)
>>> print(matches[-1])
Oder nur der zweite:
>>> print(matches[1])
Was ist das Problem? – revo
Ich bekomme einen Fehler 'AttributeError: 'NoneType' Objekt hat kein Attribut 'groups'' – dumbo
Eine mögliche dup? https://stackoverflow.com/questions/15232832/python-regex-attributeerror-nonetype-object-has-no-attribute-groups – revo