Hi,
I think I am need of a regular expression with regards to a str_replace
need that I have...
I have a string like this one...
"text text text{
text
} text {text text } t {text} txt { text }"
I need to replace all occurances of "{" with "{ " and "}" with " }" where
the following rules apply...
replace "{" with "{ " when the character to the right of "{" is NOT
<newline> and is NOT <space> and is NOT <tab>.
replace "}" with " }" when the character to the left of "}" is NOT
<newline> and is NOT <space> and is NOT <tab>.
So, the result in my example above would be....
"text text text{
text
} text { text text } t { text } txt { text }"
Would anyone possibly know how to do this?
Thanks
--
most midwives do not have children 4 1170
Disco Octopus wrote: Hi,
I think I am need of a regular expression with regards to a str_replace need that I have...
I have a string like this one...
"text text text{ text } text {text text } t {text} txt { text }"
I need to replace all occurances of "{" with "{ " and "}" with " }" where the following rules apply...
replace "{" with "{ " when the character to the right of "{" is NOT <newline> and is NOT <space> and is NOT <tab>.
replace "}" with " }" when the character to the left of "}" is NOT <newline> and is NOT <space> and is NOT <tab>.
So, the result in my example above would be....
"text text text{ text } text { text text } t { text } txt { text }"
Would anyone possibly know how to do this?
You would need to use both a zero-width negative look-ahead and a
zero-width negative look-behind with prel style regex...
<?php
$s =<<<EOS
text text text{
text
} text {text text } t {text} txt { text }
EOS;
$search1='`\{(?![\s\r\n\t])`';
$replace1='{ ';
$s=preg_replace($search1,$replace1,$s);
$search2='`(?<![\s\r\n\t])\}`';
$replace2=' }';
$s=preg_replace($search2,$replace2,$s);
echo $s;
?>
--
Justin Koivisto, ZCE - ju****@koivi.com http://koivi.com
Justin Koivisto wrote: Disco Octopus wrote: Hi,
I think I am need of a regular expression with regards to a str_replace need that I have...
I have a string like this one...
"text text text{ text } text {text text } t {text} txt { text }"
I need to replace all occurances of "{" with "{ " and "}" with " }" where the following rules apply...
replace "{" with "{ " when the character to the right of "{" is NOT <newline> and is NOT <space> and is NOT <tab>.
replace "}" with " }" when the character to the left of "}" is NOT <newline> and is NOT <space> and is NOT <tab>.
So, the result in my example above would be....
"text text text{ text } text { text text } t { text } txt { text }"
Would anyone possibly know how to do this?
You would need to use both a zero-width negative look-ahead and a zero-width negative look-behind with prel style regex...
<?php $s =<<<EOS text text text{ text } text {text text } t {text} txt { text } EOS;
$search1='`\{(?![\s\r\n\t])`'; $replace1='{ '; $s=preg_replace($search1,$replace1,$s);
$search2='`(?<![\s\r\n\t])\}`'; $replace2=' }'; $s=preg_replace($search2,$replace2,$s);
echo $s;
?>
-- Justin Koivisto, ZCE - ju****@koivi.com http://koivi.com
\r, \n, \t are covered by \s already, so there is no need to specify
them separately. The code can be further compacted by using array
arguments to preg_replace():
$search = array('`\{(?!\s)`', '`(?<!\s)\}`');
$replace= array('{ ', ' }');
$s=preg_replace($search,$replace,$s);
Chung Leong wrote: Justin Koivisto wrote: Disco Octopus wrote: Hi,
I think I am need of a regular expression with regards to a str_replace need that I have...
I have a string like this one...
"text text text{ text } text {text text } t {text} txt { text }"
I need to replace all occurances of "{" with "{ " and "}" with " }" where the following rules apply...
replace "{" with "{ " when the character to the right of "{" is NOT <newline> and is NOT <space> and is NOT <tab>.
replace "}" with " }" when the character to the left of "}" is NOT <newline> and is NOT <space> and is NOT <tab>.
So, the result in my example above would be....
"text text text{ text } text { text text } t { text } txt { text }"
Would anyone possibly know how to do this? You would need to use both a zero-width negative look-ahead and a zero-width negative look-behind with prel style regex...
<?php $s =<<<EOS text text text{ text } text {text text } t {text} txt { text } EOS;
$search1='`\{(?![\s\r\n\t])`'; $replace1='{ '; $s=preg_replace($search1,$replace1,$s);
$search2='`(?<![\s\r\n\t])\}`'; $replace2=' }'; $s=preg_replace($search2,$replace2,$s);
echo $s; ?> \r, \n, \t are covered by \s already, so there is no need to specify them separately.
That's what i thought, but I was too lazy to look it up. ;)
The code can be further compacted by using array arguments to preg_replace():
$search = array('`\{(?!\s)`', '`(?<!\s)\}`'); $replace= array('{ ', ' }'); $s=preg_replace($search,$replace,$s);
That is how I tested it, but then I pulled it apart for clarity of what
was going on.
--
Justin Koivisto, ZCE - ju****@koivi.com http://koivi.com
Chung Leong wrote: Justin Koivisto wrote: Disco Octopus wrote: Hi,
I think I am need of a regular expression with regards to a str_replace need that I have...
<....> You would need to use both a zero-width negative look-ahead and a zero-width negative look-behind with prel style regex...
<?php
<great stuff>
?> \r, \n, \t are covered by \s already, so there is no need to specify
<more great stuff> $search = array('`\{(?!\s)`', '`(?<!\s)\}`'); $replace= array('{ ', ' }'); $s=preg_replace($search,$replace,$s);
Thanks very much Juston and Chung. This works like a charm for me, and is
hugely appreciated.
--
if it itches, it will be scratched This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Lukas Holcik |
last post by:
Hi everyone!
How can I simply search text for regexps (lets say <a
href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a
dictionary { name : URL}? In a single pass if it could....
|
by: Andrew DeFaria |
last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came
across RegExp and thought I could tighten up my JavaScript code that
checks for a valid email address. Why does the following...
|
by: zzzxtreme |
last post by:
hello i have this function to check date (not mine)
function (s_date) {
// check format
if (!re_dt.test(s_date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)...
|
by: Jon Maz |
last post by:
Hi All,
I want to strip the accents off characters in a string so that, for example,
the (Spanish) word "práctico" comes out as "practico" - but ignoring case,
so that "PRÁCTICO" comes out as...
|
by: Phat G5 (G3) |
last post by:
I am weak when it comes to regexp but hoped someone might know in this case.
I am trying to take a url like this :
something.lasso?blah=blah&blah2=blah2&sort=hello&blah3=blah3
And remove the...
|
by: HopfZ |
last post by:
I coudn't understand some behavior of RegExp.test function.
Example html code:
----------------
<html><head></head><body><script type="text/javascript">
var r = /^https?:\/\//g;...
|
by: SQL Learner |
last post by:
Hi all,
I have an Access db with two large tables - 3,100,000 (tblA) and 7,000
(tblB) records. I created a select query using Inner Join by partial
matching two fields (X from tblA and Y from...
|
by: reflex |
last post by:
Hi,
i have script with pattern (href=?(.*)({1}+)?),
where i match any occurence of url and replace hyperlink so i have
only anchor in it.
var regExp = /href=?(.*)({1}+)?/ig;
var wholeContent...
|
by: Flyzone |
last post by:
Hello,
i have again problem with regexp :-P
I need to match all lines that contain one word but not contain
another.
Like to do "grep one | grep -v two:"
The syntax of the string is:
(any...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |