Python - Parsing JSON formatted text file with regex

后端 未结 4 721
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 09:34

I have a text file formatted like a JSON file however everything is on a single line (could be a MongoDB File). Could someone please point me in the direction of how I could

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 10:04

    How about using positive lookahead and lookbehind:

    (?<=\"fileAssetId\":\")[a-fA-F0-9-]+?(?=\")
    

    captures the fileAssetId and

    (?<=\"filename\":\").+?(?=\")
    

    matches the filename.

    For a detailed explanation of the regex have a look at the Regex101-Example. (Note: I combined both in the example with an OR-Operator | to show both matches at once)

    To get a list of all matches use re.findall or re.finditer instead of re.match.

    re.findall(pattern, string) returns a list of matching strings.

    re.finditer(pattern, string) returns an iterator with the objects.

提交回复
热议问题