By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,650 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,650 IT Pros & Developers. It's quick & easy.

Trying to prevent duplicates in preg_replace Regex

P: n/a
Hi all,

I'm trying to write a regex pattern to use in preg_replace. Basically
I want to put [brackets] around every line (\n) in this variable.
However, I need to exclude lines that already have brackets or
quotation marks around them as well as lines that start with a
hyphen. Lastly the lines with ** need the brackets before the **,
instead of at the end of the line.

Here is what I have so far - it is close, but I keep ending up with
duplicate brackets.

$temp = 'this
is
a
test
"this"
"is"
"a"
[this]
[is]
[a]
test ** 93.23
test 2 ** 10.05
-this
-is
-a
another good test
';
$PATTERNS = array("/^[^\"\[]/m", "/[^\"\]]$/m", "/[\r\n]+/", "/ \*\*
(.*)]/");
$REPLACEMENTS = array("[", "]", "]\n[", "] \*\* $1");
$output1 = trim(preg_replace($PATTERNS, $REPLACEMENTS, trim($temp)));
print_r($output1);

Thanks for your help!
Erik

Feb 23 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
erikcw wrote:
I'm trying to write a regex pattern to use in preg_replace. Basically
I want to put [brackets] around every line (\n) in this variable.
However, I need to exclude lines that already have brackets or
quotation marks around them as well as lines that start with a
hyphen. Lastly the lines with ** need the brackets before the **,
instead of at the end of the line.
$x ='...'; // orig string

$x = preg_replace('/^(.*)$/m', '[$1]', $x);
$x = preg_replace('/^\[\"(.*)\"\]$/m', '"$1"', $x);
$x = preg_replace('/^\[\[(.*)\]\]$/m', '[$1]', $x);
$x = preg_replace('/^\[(.*)(\s*\*\*.*)\]$/m', '[$1]$2', $x);
$x = preg_replace('/^\[\]/m', '', $x);

That last regexp covers the behaviour of empty lines, and lines that start
with a double-asterisk.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 23 '07 #2

P: n/a
erikcw wrote:
Here is what I have so far - it is close, but I keep ending up with
duplicate brackets.
[...]

Often, simplicity = bless:

$PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/');
$REPLACEMENTS = array('', '[$1]');
JW
Feb 24 '07 #3

P: n/a
On Feb 24, 9:45 am, "Janwillem Borleffs" <j...@jwscripts.comwrote:
erikcw wrote:
Here is what I have so far - it is close, but I keep ending up with
duplicate brackets.

[...]

Often, simplicity = bless:

$PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/');
$REPLACEMENTS = array('', '[$1]');

JW
Thank you for your reply.

Your selution is simple and elegant. Unfortunatly, it puts brackets
around every line. I need it to ignore lines with "" around them, as
well as line that start with a '-' and lines that end in a ** 0.00
need the ending bracket before the **.

So, the output should look like this:
$input = 'this
is
a
test
"this"
"is"
"a"
[this]
[is]
[a]
test ** 93.23
test 2 ** 10.05
-this
-is
-a
another good test
';

$OUTPUT = '[this]
[is]
[a]
[test]
"this"
"is"
"a"
[this]
[is]
[a]
[test] ** 93.23
[test 2] ** 10.05
-this
-is
-a
[another good test]
';

What needs to be changed in your pattern to do this?

Thanks so much!
Erik

Feb 24 '07 #4

P: n/a
Rik
erikcw <er***********@gmail.comwrote:
Your selution is simple and elegant. Unfortunatly, it puts brackets
around every line. I need it to ignore lines with "" around them, as
well as line that start with a '-' and lines that end in a ** 0.00
need the ending bracket before the **.
Well, it can be done, but it will be quite cumbersome/clumsy. Let's
backtrack: why do you need the data like that?
--
Rik Wasmus
Feb 24 '07 #5

P: n/a
On Feb 24, 1:31 pm, Rik <luiheidsgoe...@hotmail.comwrote:
erikcw <erikwickst...@gmail.comwrote:
Your selution is simple and elegant. Unfortunatly, it puts brackets
around every line. I need it to ignore lines with "" around them, as
well as line that start with a '-' and lines that end in a ** 0.00
need the ending bracket before the **.

Well, it can be done, but it will be quite cumbersome/clumsy. Let's
backtrack: why do you need the data like that?
--
Rik Wasmus
I need to format the data this way for a legacy system.

Feb 24 '07 #6

P: n/a
On Feb 24, 1:54 pm, "erikcw" <erikwickst...@gmail.comwrote:
On Feb 24, 1:31 pm, Rik <luiheidsgoe...@hotmail.comwrote:
erikcw <erikwickst...@gmail.comwrote:
Your selution is simple and elegant. Unfortunatly, it puts brackets
around every line. I need it to ignore lines with "" around them, as
well as line that start with a '-' and lines that end in a ** 0.00
need the ending bracket before the **.
Well, it can be done, but it will be quite cumbersome/clumsy. Let's
backtrack: why do you need the data like that?
--
Rik Wasmus

I need to format the data this way for a legacy system.
What about this:
$PATTERNS = array('/([^\r\n]+)/', '/\[\[(.*)]]/', '/\["(.*)"]/', '/\[-
(.*)]/', '/ \*\* (.*)]/');
$REPLACEMENTS = array('[$1]', '[$1]', '"$1"', '-$1', '] ** $1');

I guess the question is, is it faster to run several small patterns or
to run a single larger pattern that handles all possible cases?

Thanks for your help!
Erik

Feb 24 '07 #7

P: n/a
erikcw wrote:
Your selution is simple and elegant. Unfortunatly, it puts brackets
around every line. I need it to ignore lines with "" around them, as
well as line that start with a '-' and lines that end in a ** 0.00
need the ending bracket before the **.
Perhaps with some fix up patterns:

$PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/', '/(\["|"\])/',
'/(\[)\-(.+)(\])/',
'/(\[)(.+)([*]+)(.+)(\])/');
$REPLACEMENTS = array('', '[$1]', '"', '-$2', '[$2$3]$4');
JW
Feb 25 '07 #8

P: n/a
Janwillem Borleffs wrote:
$PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/', '/(\["|"\])/',
'/(\[)\-(.+)(\])/',
'/(\[)(.+)([*]+)(.+)(\])/');
$REPLACEMENTS = array('', '[$1]', '"', '-$2', '[$2$3]$4');
Err:

$PATTERNS = array('/(\[|\])/', '/([^\r\n]+)/', '/(\["|"\])/',
'/(\[)\-(.+)(\])/',
'/(\[)(.+)([*]{2})(.+)(\])/');
$REPLACEMENTS = array('', '[$1]', '"', '-$2', '[$2]$3$4');
JW
Feb 25 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.