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 8 2250
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!
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
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
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
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.
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
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
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Justin Koivisto |
last post: by
|
6 posts
views
Thread by JDJones |
last post: by
|
5 posts
views
Thread by paul brown |
last post: by
|
3 posts
views
Thread by JaNE |
last post: by
|
3 posts
views
Thread by Charles |
last post: by
|
22 posts
views
Thread by stoppal |
last post: by
|
16 posts
views
Thread by Andrew Baker |
last post: by
|
7 posts
views
Thread by monomaniac21 |
last post: by
| | | | | | | | | | | |