2

I read a file having the following text

    this is::b(test file).
    extra this is::another(test file).
    this is::a(test file)

I am able to read the file and write it in another file.I want to capture the word after

this is:: and before '(' i.e 'b' from the first line and 'a' from the 3rd line and store it in a list and nothing from the 2nd line since it has extra word before 'this is' I tried using the Regular expression:

   for item in lines:
     print(item)
     fw.write(item)
    found=None

found=(re.search('this is::(.+?)[)]',y).group(1)

where y= string read from the file

y=''.join(lines)     

But it is capturing only 'b' and adding the new line after each line.

Can someone please suggest how should I go about achieving it. Expected File:

 this is::b(test file).
 An extra line:b
 extra this is:another(test file).
 this is::a(test file).
 An extra line:a
Saurabh
  • 37
  • 4
  • 14
  • Please add an example of your expected results. – SteveJ Nov 28 '17 at 23:58
  • My Expected File Should Looks like this : this is::b(test file). "Extra line" extra this is::another(test file). this is::a(test file) ."Extra line" – Saurabh Nov 29 '17 at 00:46
  • Use the regex: [`^[ ]*this\s*is::([^(\n]+)(?=\()`](https://regex101.com/r/J2XPqK/1) and fetch the value of Group1 – Gurmanjot Singh Nov 29 '17 at 03:15
  • @Saurabh; I've updated my answer to reflect your desired results -- I'm not sure why you were choosing to join the lines, nor does your actual result make sense given the code you provided, so I can't tell you specifically what is wrong with your code. – SteveJ Nov 29 '17 at 15:38

2 Answers2

1

I think this is along the lines of what you are after?

[Edit: Updated code to reflect change to the question]

import re

lines = ("this is::b(test file).",
         "extra this is::another(test file)",
         "this is::a(test file)",
         "this is::another test")

words = []
for line in lines:
    words.append(line)
    found = re.search('^this is::.*[(]', line)
    if found is None: continue
    word = line.split('::')[1].split('(')[0]
    words.append(f"An extra line:{word}")

for word in words:  
    print(word)  # You can save to file here instead of print

Yields

this is::b(test file).
An extra line:b
extra this is::another(test file)
this is::a(test file)
An extra line:a

Note, I gave a regex for the simplest case; If you need a better regex -- you owe it to yourself to work that out independently. Here is a great tool, debuggex, for helping test regex expressions.

Cheers

SteveJ
  • 3,034
  • 2
  • 27
  • 47
0

For capturing text

For your example, you can search for the string literal this is:: then create a sub pattern (.*?) (the ? makes it non-greedy).

Example

import re 

s =  """
this is::b(test file).
extra this is::another(test file).
this is::a(test file)
"""

res = re.findall("this is::(.*?)\((.*?)\)", s)
for r in res:
    print(r)

Output

('b', 'test file')
('another', 'test file')
('a', 'test file')

For appending line to a file

I'm not clear on what you want here, but if you simply want to append to a file... see this post

Example

with open("test.txt", "a") as myfile:
    myfile.write("Found something!")
Alter
  • 3,332
  • 4
  • 31
  • 56