in reply to Re^2: Communication of program(s) with regex codeblocks
in thread Communication of program(s) with regex codeblocks

The ideal container program would slurp the regexfile, then apply it to whatever text that is to be searched. In the case of a match, it would somehow be able to retrieve the captured values in the proper order.

Doesn't the following do just that?

my $re = do { local *FILE; open(FILE, '<', $regexp_file_name) or die('...'); local $/; qr/@{[<FILE>]}/ # Compile only once. }; while (<DATA>) { if (@captures = $_ =~ $re) { print(join(', ', @captures), $/); } } __DATA__ abd 123 sdafas 231 gdabd 7364 112 sdafas 785 regexp file (Matches lines with two words of exactly 3 digits.) =========== \b(\d{3})\b.*?\b(\d{3})\b output ====== 123, 231 112, 785

Replies are listed 'Best First'.
Re^4: Communication of program(s) with regex codeblocks - (explaining ultimate goal)
by erix (Prior) on Oct 28, 2004 at 21:26 UTC

    My 'specification' was incomplete, and I apologise for that. I will give some context to make clear what I am trying to figure out.

    Ultimate goal: extracting information from a wide variety of text files, representing published articles.

    With a largish set of text files, most of them needing their own set of regexes (to split them into usable parts), it would seem to make sense to store regexfile with textfile. I foresee that a lot of strings thus extracted will subsequently (to get that final info-nugget for my database) need unique code. It would make sense to also keep such code associated with the textfile. For this, the codeblocks are one candidate, modules are another, snippet files for future eval yet another.

    (a concrete example of the textbase: from one author all published articles, 600 text files, ranging from 1 page to 100+ pages, published over a period of 40 years in several journals.)

    So there is the context in which I am trying to find out how 'heavy' those regex codeblocks can be 'loaded' with code, and what communication/steering/instrumentation can be dreamed up. I initially hoped that DBI searches would be possible. This turns out to be almost certainly impossible/unwise, because absolutely no regex can be used inside a codeblock. But I believe subroutines and closures can be defined and called.

    So what I'm trying is to logically split very 'specific' code over a text-centric system with many disparate textfiles. It may be a Bad Thing - I am not advocating it, but hope to gain from experience of others on similar exploits.

      Are you saying you're planning on writting a regexp for every file?

      • If so, you could include a code block at the top, and seperate it from the data with __DATA__. Then the code block can process the data using <DATA>. (See my post further up this thread for an example on how to use __DATA__.)
      • If not, I don't see why your program has to be in a regexp instead of the other way around.