Link to home
Start Free TrialLog in
Avatar of mark_D74
mark_D74Flag for Ireland

asked on

Insert a string before every substring matching a particular pattern

Hi all,

I have a big bunch of text (Copied and pasted from bank statements in PDF format).  I need to format this into a CSV file but have hit a snag.  Each line should start with a date, formatted as follows:
01.11 02.11 (There are two date columns side-by-side with no year).  
I need a way to "search and replace" these patterns, inserting a <br /> in front of each date, but obviously retaining the date.  I've gotten as far as finding that I need to use regular expressions. But, I haven't been able to append my <br /> to the start of the date. Anyone any ideas?


Avatar of twohawks
twohawks
Flag of United States of America image

You could use php's strtok

$string = "01.11 02.11";

$tok = strtok($string, " \n\t");

while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}

Open in new window


http://de3.php.net/manual/en/function.strtok.php

If necessary, could you post some code for a little better context to work off of for providing more concise assistance.
Sometimes I get ahead of myself... my suggestion is pointing you in the wrong direction, sorry.  I think you are going to want to use explode() with a regex.  I am looking into that now, but I bet soemone is already posting the answer (EE daemons never sleep ;^)
ASKER CERTIFIED SOLUTION
Avatar of twohawks
twohawks
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw, I use a space character for the column break
So for instance, if you are pulling tab-delimited text from teh csv, you could use (\t) in place of ( ), like so...

^([0-9][0-9]\.[0-9][0-9])(\t)([0-9][0-9]\.[0-9][0-9])(.*)$

Please express if this line of responses is resonating with what you seek, and if so, provide a little better example of input/output_result requirement if you need more help on this, like formulating the php (or whatever) ;^)
@mark_D74: please have a look at this article.  It's not really an answer to your question but it may help you understand why we need to see some representative test data in order to be of help to you here.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

"I have a big bunch of text...  need to format this into a CSV file...  Each line should start with a date..." -- we need a little more concrete information about what you have got now and what you want to get out of it.  So please show us some of the inputs and show us what the outputs should look like, thanks. ~Ray
Avatar of mark_D74

ASKER

@Ray - I had already managed to isolate all of the bits of data I needed except for the date, which I was having trouble with.  With the help of twohawks, I fairly quickly cracked that bit of the puzzle also and the resulting script works perfectly.  However, I suspect if I had started off as you said by giving a sample of the data instead of working it all out myself (which took a couple of hours) someone who lives, eats and sleeps PHP on a daily basis might have done the whole thing for me in a couple of minutes.  
Glad that helped, Mark. Thanks for the points.