[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

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?


0
mark_D74
Asked:
mark_D74
  • 6
1 Solution
 
twohawksCommented:
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.
0
 
twohawksCommented:
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 ;^)
0
 
twohawksCommented:
In regex we use what's called a backreference. Essentially you can tag groups of strings within your search pattern.

===============
Regx Search Pattern
^([0-9][0-9]\.[0-9][0-9])( )([0-9][0-9]\.[0-9][0-9])(.*)$

Search text
01.11 02.10 aldfja a;ldf a;lf a;oai had[foin ab
01.11 02.10 aldfja a;ldf a;lf a;oai had[foin ab

Result
01.11<br />02.10<br /> aldfja a;ldf a;lf a;oai had[foin ab
01.11<br />02.10<br /> aldfja a;ldf a;lf a;oai had[foin ab

================
Alternate Search Pattern
\1<br />\n\3<br />\4

Result
01.11<br />
02.10<br /> aldfja a;ldf a;lf a;oai had[foin ab
01.11<br />
02.10<br /> aldfja a;ldf a;lf a;oai had[foin ab


================
and so on.


Without a better example for what you need, this is what I came up with.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
twohawksCommented:
btw, I use a space character for the column break
0
 
twohawksCommented:
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) ;^)
0
 
Ray PaseurCommented:
@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.
http://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
0
 
mark_D74Author Commented:
@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.  
0
 
twohawksCommented:
Glad that helped, Mark. Thanks for the points.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now