473,656 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_repla ce($PATTERNS, $REPLACEMENTS, trim($temp)));
print_r($output 1);

Thanks for your help!
Erik

Feb 23 '07 #1
8 2362
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.comwro te:
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.comwro te:
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
2984
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) $x=preg_replace('/--PRICE-LIST--/',$list,$x,1); OK, so what this does it is takes each array element and replaces only the first occurrance of "--PRICE-LIST--" with it. I would have used str_replace, but I didn't think it should be necessary to create an array...
6
2715
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(";", ",", $question1); Is there a better way to replace it in all the $_POST variables at once like maybe $_POST - preg_replace(";", ",", $_POST);
5
2113
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
3166
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 underline tag, it doesn't replace body, div, span tags... Can anyone help me with this? $txt = preg_replace('/<\/?>/i', ' ', $txt); and problem more: I would like to keep <br> tag and would like to change <p> to <br> (and discard </p>)......
3
5321
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 --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
22
2756
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, $results)
16
2150
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 RegEx.IsMatch but it behaves quite differently. Is there a way I can mimic the DOS filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls" returns all excel files, "workbook*" returns all files begining with "workbook" etc)? thanks in...
7
2031
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 with i or ii as in 320ii and i want to strip out the i's at the end when displaying the product name
7
16154
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 the domain name www.blog-start.com Unfortuantely it is not not working correctly
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.