Python Forum
please help with regex search.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help with regex search.
#1
i have problem with regex, i can't find a way get full line in doc with keyword.
i want to get result show"{"cid": "Ugfewffgikbsfewf3BAg", "text": "五男科永远不欢迎你", "time": "4 months ago", "author": "海wbeE", "votes": "1", "photo": "https://yt3.ggpht.com/a/AATXAJzjqGwefwef3Lrt5g4fhElmbFYC7o05S-ycUnlQ=s48-c-k-c0xffffffff-no-rj-mo"}" with keyword inside.
result same as linux commandline ' cat doc | grep keyword' please advice. thanks.
import re
doc = '''
{"cid": "Ugfewffgikbsfewf3BAg", "text": "洲男永远不欢迎你", "time": "4 months ago", "author": "海wbeE", "votes": "1", "photo": "https://yt3.ggpht.com/a/AATXAJzjqGwefwef3Lrt5g4fhElmbFYC7o05S-ycUnlQ=s48-c-k-c0xffffffff-no-rj-mo"}\n'
{"cid": "Ugfewffgikbsfewf3BAg", "text": "五科永远不欢迎所有人", "time": "4 months ago", "author": "海wbeE", "votes": "1", "photo": "https://yt3.ggpht.com/a/AATXAJzjqGwefwef3Lrt5g4fhElmbFYC7o05S-ycUnlQ=s48-c-k-c0xffffffff-no-rj-mo"}\n'
{"cid": "Ugfewffgikbsfewf3BAg", "text": "男科永远不欢迎你地", "time": "4 months ago", "author": "海wbeE", "votes": "1", "photo": "https://yt3.ggpht.com/a/AATXAJzjqGwefwef3Lrt5g4fhElmbFYC7o05S-ycUnlQ=s48-c-k-c0xffffffff-no-rj-mo"}\n'
{"cid": "Ugfewffgikbsfewf3BAg", "text": "五洲永远不欢迎你们", "time": "4 months ago", "author": "海wbeE", "votes": "1", "photo": "https://yt3.ggpht.com/a/AATXAJzjqGwefwef3Lrt5g4fhElmbFYC7o05S-ycUnlQ=s48-c-k-c0xffffffff-no-rj-mo"}\n'
'''
with open(doc, 'r', encoding='utf-8') as f:
    file=f.readlines()
    keywords = '欢迎'
    pattern = re.findall(r'[^,。]+[a-zA-Z0-9| \u4e00-\u9fa5]*{}[a-zA-Z0-9| \u4e00-\u9fa5]*'.format(keywords), str(file))
    for match in pattern:
        print("\n")
        print(match)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 1,570 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  search file by regex SamLiu 1 938 Feb-23-2023, 01:19 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020