473,396 Members | 2,061 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.

Replace all but the last one.

To replace just the last full stop in some text with ".@" I can use

preg_replace('/\.$/','.@',$s);

But how so I reverse that to replace all full stops except for the
last one.

E.G.
"Hello I must be going.
And so it goes. On and on."
should become:
"Hello I must be going.@
And so it goes.@ On and on."
Aug 28 '08 #1
7 4410
Captain Paralytic wrote:
To replace just the last full stop in some text with ".@" I can use

preg_replace('/\.$/','.@',$s);

But how so I reverse that to replace all full stops except for the
last one.

E.G.
"Hello I must be going.
And so it goes. On and on."
should become:
"Hello I must be going.@
And so it goes.@ On and on."
Do you mean remove the last occurrence of ".@", or only remove ".@" if
it's at the end of the string?

Your regex replace doesn't work, so I changed it up. Here are a couple
examples:

Remove if .@ is at end of string:

<?php
$s = "Hello I must be going.\n" .
"And so it goes. On and on.';

$s = preg_replace('/\.(\s|$)/', '.@$1', $s);
$s = rtrim($s, '.@') . '.';
?>

Remove last occurrence of .@ in the string (might not be at end):

<?php
$s = "Hello I must be going.\n" .
"And so it goes. On and on.';

$s = preg_replace('/\.(\s|$)/', '.@$1', $s);
$s = substr_replace($s, '.', strrpos($s,'.@'), 2);
?>

HTH

--
Curtis (http://dyersweb.com)
Aug 29 '08 #2
Curtis wrote:
Captain Paralytic wrote:
>To replace just the last full stop in some text with ".@" I can use

preg_replace('/\.$/','.@',$s);

But how so I reverse that to replace all full stops except for the
last one.

E.G.
"Hello I must be going.
And so it goes. On and on."
should become:
"Hello I must be going.@
And so it goes.@ On and on."

Do you mean remove the last occurrence of ".@", or only remove ".@" if
it's at the end of the string?

Your regex replace doesn't work, so I changed it up. Here are a couple
examples:
<snip>

Sorry, just a follow up. The reason the regex didn't work for me was
because I'm on win32, and the line terminators were CRLFs, which the
/m modifier won't recognize. Setting the text editor to UNIX format or
explicitly using LFs make it work properly.

Sorry, again.

--
Curtis
Aug 29 '08 #3
On 29 Aug, 02:53, Curtis <dye...@gmail.comwrote:
Captain Paralytic wrote:
To replace just the last full stop in some text with ".@" I can use
preg_replace('/\.$/','.@',$s);
But how so I reverse that to replace all full stops except for the
last one.
E.G.
"Hello I must be going.
And so it goes. On and on."
should become:
"Hello I must be going.@
And so it goes.@ On and on."

Do you mean remove the last occurrence of ".@", or only remove ".@" if
it's at the end of the string?
No, I don't mean that at all.

I have:
Hello I must be going.
And so it goes. On and on.

If I run it through:
preg_replace('/\.$/','.@',$s);

I get:
Hello I must be going.
And so it goes. On and on.@

So that pattern replaces the last occurrence (and only if that last
occurrence is at the end of the string) of a full stop with .@

If instead I run it through:
preg_replace('/\./','.@',$s);

I get:
Hello I must be going.@
And so it goes.@ On and on.@

HOWEVER, what I want to get is:
Hello I must be going.@
And so it goes.@ On and on.

Is this any clearer?
Aug 29 '08 #4
In article <66042273-66fd-41df-b9bb-3ef0ec275170
@k37g2000hsf.googlegroups.com>, pa**********@yahoo.com says...
On 29 Aug, 02:53, Curtis <dye...@gmail.comwrote:
Captain Paralytic wrote:
To replace just the last full stop in some text with ".@" I can use
preg_replace('/\.$/','.@',$s);
But how so I reverse that to replace all full stops except for the
last one.
E.G.
"Hello I must be going.
And so it goes. On and on."
should become:
"Hello I must be going.@
And so it goes.@ On and on."
Do you mean remove the last occurrence of ".@", or only remove ".@" if
it's at the end of the string?
No, I don't mean that at all.

I have:
Hello I must be going.
And so it goes. On and on.

If I run it through:
preg_replace('/\.$/','.@',$s);

I get:
Hello I must be going.
And so it goes. On and on.@

So that pattern replaces the last occurrence (and only if that last
occurrence is at the end of the string) of a full stop with .@

If instead I run it through:
preg_replace('/\./','.@',$s);

I get:
Hello I must be going.@
And so it goes.@ On and on.@

HOWEVER, what I want to get is:
Hello I must be going.@
And so it goes.@ On and on.

Is this any clearer?
Ok, then try my example in my first reply that uses rtrim, that should
do the trick.

--
Curtis
Aug 30 '08 #5
On 30 Aug, 01:57, Curtis Dyer <dye...@gmail.comwrote:
In article <66042273-66fd-41df-b9bb-3ef0ec275170
@k37g2000hsf.googlegroups.com>, paul_laut...@yahoo.com says...


On 29 Aug, 02:53, Curtis <dye...@gmail.comwrote:
Captain Paralytic wrote:
To replace just the last full stop in some text with ".@" I can use
preg_replace('/\.$/','.@',$s);
But how so I reverse that to replace all full stops except for the
last one.
E.G.
"Hello I must be going.
And so it goes. On and on."
should become:
"Hello I must be going.@
And so it goes.@ On and on."
Do you mean remove the last occurrence of ".@", or only remove ".@" if
it's at the end of the string?
No, I don't mean that at all.
I have:
Hello I must be going.
And so it goes. On and on.
If I run it through:
preg_replace('/\.$/','.@',$s);
I get:
Hello I must be going.
And so it goes. On and on.@
So that pattern replaces the last occurrence (and only if that last
occurrence is at the end of the string) of a full stop with .@
If instead I run it through:
preg_replace('/\./','.@',$s);
I get:
Hello I must be going.@
And so it goes.@ On and on.@
HOWEVER, what I want to get is:
Hello I must be going.@
And so it goes.@ On and on.
Is this any clearer?

Ok, then try my example in my first reply that uses rtrim, that should
do the trick.

--
Curtis- Hide quoted text -

- Show quoted text -
I was already doing something similar. It just seemed to me that there
ought to be a way to "reverse" the action of the pattern so as to do
it in one go.
Aug 30 '08 #6
Captain Paralytic:
[...] replace all full stops except for the last one.
/\.(?=.*\..*$)/s

--
Jock
Aug 30 '08 #7
John Dunlop wrote:
Captain Paralytic:
>[...] replace all full stops except for the last one.

/\.(?=.*\..*$)/s

--
Jock
Simpler yet, this will only replace periods followed by one or more
whitespace chars, but will not replace a period at the end of the string:

preg_replace('/\.(\s+)/', '.@$1', $str);

--
Curtis
Sep 1 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Billy N. Patton | last post by:
-------- Original Message -------- Subject: <string>.replace Date: Fri, 15 Oct 2004 11:07:19 -0500 From: Billy N. Patton <b-patton@ti.com> Organization: Texas Instruments Newsgroups:...
1
by: Chi Tang | last post by:
Hi, Does anybody know where I can find samples to search an element in a subtree of an xml file and replace some new value for this element? I can only find some samples to use 'replace last...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
17
by: Randy Webb | last post by:
I know that the /g flag will match all occurrences. Is there a way, with a Regular Expression, to match all occurrences *except* the last one? pattern = /df/g; var myString = "asdfasdfasdfasdf";...
1
by: Seema Multani | last post by:
I have a string "1152090116" I want to replace last digit of the string from 6 to 0. e.g my resulted string would be "1152090110". How can I do that. Thanks in advance
8
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why?...
4
by: pnsreee | last post by:
Hi all, I have a string "Post.lang.tmp.txt" and i have to replase the ".txt" with "_large.txt". Please help me regarding this problem. I got it in shell scripting using `echo $string |...
5
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
IDE 2003 (C#) need to replace last string by a carriage return line feed find themethod(); replace it by themethod(); int i++; thanks
15
by: =?Utf-8?B?TWlrZSAiWU9fQkVFIiBC?= | last post by:
I have a text file that contains about 8 to 10 text sequences that I need to replace. I want to search and replace all 8 to 10 text sequence anytime I run this script Here is what I have so...
5
by: shapper | last post by:
Hello, I have a text as follows: "My email is something@something.xyz and I posted this @ 2 am" I need to replace the @ by (AT) bu only the ones that are in email addresses. All other @...
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?
0
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...
0
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,...
0
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...
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
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...
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,...

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.