Python Forum
Help with generating regx Pattern please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with generating regx Pattern please
#1
Hi all, i need to extract "Names" from a Textfile
which i liked to solve via Regx, but i fail to generate the correct pattern for that. Can somebody please help me out ?

i need to extract after the second Number until the end of Line

˜16524:˜ 1 §š£™£ž£LÁ-SÔÙÌEž£™£š£¥
˜11241:˜ 159 —.M˜A›CžX.

to
§š£™£ž£LÁ-SÔÙÌEž£™£š£¥
—.M˜A›CžX.

but my current output is

♣:˜ 1 ▼§š£™£ž£♣LÁ-SÔÙÌEž£™£š£▼¥
♣:˜ 159 —.M˜A›CžX♣.

A sample file with 21 Lines is attached and my current code looks like :

import re

def parse_username(content):
    print(f"Content after seconds: {content.strip()}")

def process_file(file_path, encoding='utf-8'):
    pattern = re.compile(r'\d+(.*)')
    
    try:
        with open(file_path, 'r', encoding=encoding) as file:
            for line in file:
                match = pattern.search(line)
                if match:
                    content_after_number = match.group(1)
                    parse_username(content_after_number)
    except UnicodeDecodeError as e:
        print(f"Error decoding file: {e}")
    except FileNotFoundError:
        print(f"File not found: {file_path}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

process_file('names.txt')
Thanks in Advance

Attached Files

.txt   names.txt (Size: 766 bytes / Downloads: 4)
Reply


Messages In This Thread
Help with generating regx Pattern please - by lastyle - Aug-26-2024, 08:14 PM

Forum Jump:

User Panel Messages

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