Note that there are some explanatory texts on larger screens.

plurals
  1. POSave EACH regex match to a new txt file (batch)?
    primarykey
    data
    text
    <p>I basically need a program/script that will search a file for regex matches and then save each match to a newly created text file (ie. match_01.txt, match_02.txt, match_03.txt, etc.). NB: it must support multiline matching!</p> <p><strong>EDIT :</strong></p> <p>This is what I tried using Josha's help (thx:):</p> <p>I get an error when I try this</p> <p><strong>Python Script:</strong> </p> <pre><code>import re pattern = re.compile(r'(?s)(?&lt;=Sample)(.*?)(?=EndSample)', flags=re.S) with open('test.txt', 'r') as f: matches = pattern.findall(f.read()) for i, match in enumerate(matches): with open('Split/match{0:04d}.txt'.format(i), 'w') as nf: nf.write(match) </code></pre> <p></p> <p><strong>Command Prompt:</strong></p> <pre><code>C:\Test\python test.py Traceback (most recent call last): File "test.py", line 31, in &lt;module&gt; nf.write(match) TypeError: expected a character buffer object </code></pre> <p>test.txt looks something like this:</p> <p>Sample A1 ... ... ... ... ... EndSample</p> <p>Sample B4 ... ... ... ... ... EndSample</p> <p>Sample X6 ... ... ... ... ... EndSample</p> <p>So I need to match everything between "Sample" and "EndSample" (hundreds of lines in-between) and write each match to its own txt file. So far it only works if my regex pattern is ie. "Sample". There is 15 matches and it does create 15 txt files in the Split folder but they all contain just the word Sample and nothing more. Multiline still not working looks like.. And if my regex is this: </p> <blockquote> <p>(?s)(Sample)(.*?)</p> </blockquote> <p>then it also gives me the same error as above. Its like it doesnt like (.*?) Strange..?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload