473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trying to prevent duplicates in preg_replace Regex

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
8 2345
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Justin Koivisto | last post by:
I am replacing a string in a text block that has a literal $ in it, and preg_replace is seeing it as a backreference. Here is what I am using: foreach($price_lists as $list)...
6
by: JDJones | last post by:
Just want to verify. I have a form and I want to parse any semi-colons out of the submitted info and replace with commas. Would this be the correct way to do it? $question1 = preg_replace(";",...
5
by: paul brown | last post by:
howdy, I have some text: This is {search}Pam Grier{/search}. What I want is this: This is <a href="somepage.php?keyword=Pam Grier">Pam Grier</a>.
3
by: JaNE | last post by:
I need to replace all possible html tags in user input but don't want to replace bold and italic start and end tags, so I made folowing line, but it replaces only "one-letter-tags" like paragraf or...
3
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this ...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
7
by: monomaniac21 | last post by:
hi all using preg_replace how can i replace the letter i in a string with nothing (delete it) when it is the last letter or it is followed by an i? i have products that are listed in a db...
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.