Jump to content

Add delimiter to regular expression


odonel

Recommended Posts

Hello

 

I have a text file with many lines of text in this format.

 

Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt

 

I'd like to break them like this (date|~|username|~|message) so that I can insert each line in mysql  the delimiter I want to add is |~|

 

This is my first attempt to break the strings first, but got lost in the regex

 

$string1="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt";

 

preg_match_all('/(\w{3}\s\w{3}\s\d{2}\s\d{2}:\d{2}:\d{2}\s\w{3}\s\d{4},)|(\w{1,15}.*\()-/', $string1, $match, PREG_PATTERN_ORDER);

 

Link to comment
Share on other sites

Do you want to be something like this:

http://php.net/manual/en/function.preg-replace.php

$string="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt";
$patterns = array();
$patterns[0] = '/\,/';
$patterns[1] = '/(?<=all)\:/';
$replacements = array();
$replacements[1] = '~';
$replacements[0] = '~';
echo preg_replace($patterns, $replacements, $string);

Link to comment
Share on other sites

Thanx jazz, with your help, I added 2 patterns, but could not get the IP address by itself

 

$string="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt";

$patterns = array();

$patterns[0] = '/\,/';

$patterns[1] = '/(\(.*\))/';

$patterns[2] = '/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/';

$patterns[3] = '/(?<=all)\:/';

 

 

$replacements = array();

 

 

$replacements[0] = '!~';

$replacements[1] = '!~';

$replacements[2] = '!~';

$replacements[3] = '!~';

 

 

echo preg_replace($patterns, $replacements, $string);

 

 

Link to comment
Share on other sites


$string="Wed Jul 04 08:23:55 EDT 2012, johndoe(192.168.0.190) to all: testeeeeeeeeeeeeeeeeettttttttttttttttttttt";
preg_match('~([^,]+),\s*([^(]+)\(([^)]+)\)[^:]+.*)~',$string,$parts);
array_shift($parts);
// unset($parts[2]); // uncomment this line if you do NOT want IP address included
$string = implode('|~|',$parts);
echo $string;

 

Output:

Wed Jul 04 08:23:55 EDT 2012|~|johndoe|~|192.168.0.190|~| testeeeeeeeeeeeeeeeeettttttttttttttttttttt

Link to comment
Share on other sites

p.s. - storing delimited values in a single cell in a db table is generally a bad idea.  It will make it a lot harder for you later on if you want to write something that queries the db looking for individual values.  I highly recommend you store your data in separate columns instead of a single column.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.