Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex script in python or perl

Tags:

python

regex

perl

It would really make my work easier if someone could help me with writing script in python or perl in which from given file it retreives all sentences like:

[LANG::...]
  • ... means anything

for ecxample:

[LANG::Sample text with digits 0123]

and writes it to the fileeach in single line.

Thanks very much for help

EDIT:

Thanks for help, and now something more advanced.

if it finds something like [:ANG:: ...] please write only ... without brackets ang LANG:: tag.

Thanks guys You are awesome :)

like image 633
gruber Avatar asked Apr 25 '26 10:04

gruber


1 Answers

import re

with open('input.txt', 'w') as f:
    text = f.read()
#text = 'Intro [LANG::First text 1] goes on [LANG::Second text 2] and finishes.'

with open('output.txt', 'w') as f:
    for match in re.findall('\[LANG::.*?\]', text):
        f.write(match+'\n')

outputs:

[LANG::First text 1]
[LANG::Second text 2]

Second part of the question: if it finds something like [:ANG:: ...] please write only ... without brackets and LANG:: tag.

Change the last part to:

with open('output.txt', 'w') as f:
    for match in re.findall('\[.ANG::.*?\]', text):
        if match.startswith('[:ANG'):
            f.write(match[7:-1]+'\n')
        else:
            f.write(match+'\n')

Fix that substring part match[7:-1] to your needs.

like image 147
eumiro Avatar answered Apr 27 '26 00:04

eumiro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!