473,624 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regex question: replace things ONCE

Dear readers,
i need help on a regular expression:
i have to apply 100 different replaces to an article, but i don't want
that a portion of text already replaced is replaced another time by
another text. the php function strtr() does this but for other reasons
i have to use ereg_replace or preg_replace

to make you understand what i need i paste a portion of the strtr()
function documentation:
-------------------------------------------------------
strtr() will always look for the longest possible match first and will
*NOT* try to replace stuff that it has already worked on.
-------------------------------------------------------


so if i have the following replaces:

[cat]=<a href="#cat">cat </a>
[catalitic] =<a href="#cataliti c">catalitic </a>

for the following text= "my cat is catalitic"
so the result should be: my <a href="#cat">cat </ais <a
href="#cataliti c">catalitic </a>
and not: <a href="#cat">cat </ais <a href="#<a href="#cat">cat </
a>alitic"><a href="#cat">cat </a>alitic</a>

i can't use strtr() for a reason that's too long to explain

thank you very much! simone
Jun 2 '08 #1
2 1475
Greetings, Giakko.
In reply to Your message dated Tuesday, May 13, 2008, 16:03:28,
so if i have the following replaces:
[cat]=><a href="#cat">cat </a>
[catalitic] =><a href="#cataliti c">catalitic </a>
for the following text= "my cat is catalitic"
so the result should be:
my <a href="#cat">cat </ais <a href="#cataliti c">catalitic </a>
and not:
<a href="#cat">cat </ais <a href="#<a href="#cat">cat </a>alitic"><a href="#cat">cat </a>alitic</a>
There's a simple way to solve Your problem.
Use regexp modifiers to point that You have the whole word to replace, not a
part of word.
One of them are \b - word boundary.
something like

<?php

$text = 'my cat is catalitic';

$replace = array(
'#\bcat\b#',
'#\bcatalitic\b #'
);

echo preg_replace($r eplace, '<a href="#$0">$0</a>', $text);

?>
--
Sincerely Yours, AnrDaemon <an*******@free mail.ru>

Jun 2 '08 #2
Giakko wrote:
Dear readers,
i need help on a regular expression:
i have to apply 100 different replaces to an article, but i don't want
that a portion of text already replaced is replaced another time by
another text.
Just do it in one pass. preg_replace() don't check again already
processed part of string.
Jun 2 '08 #3

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

Similar topics

12
1981
by: Peter Kleiweg | last post by:
I want to use regular expressions with less typing. Like this: A / 'b.(..)' # test for regex 'b...' in A A # get the last whole match A # get the first group in the last match A /= 'b.','X',1 # replace first occurence of regex 'b.' # in A with 'X' A /= 'b.','X' # replace all occurences of regex 'b.' # in A with 'X'
3
2075
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
1
5335
by: Ada | last post by:
i'm trying to use Regex to match a 4 number group pattern. once a match is found, write it to RichTextBox in red. test data: This is my 4567 test data. 1234this is another line of data. Here's two more 9876 5432. What a headache!!!
17
3960
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher http://forta.com/books/0672325667/
9
4243
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression). Why does this function replace the found reg ex. with the actual string "\t" and not a tab? (in the example below out of frustration I actually hardcoded the "\t") Private Shared Function replaceAll(ByVal strIn As String, ByVal strFind As...
6
5880
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead". This parrot is sleeping. Really, it is sleeping. to This parrot is dead. Really, it is dead.
4
3141
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I want to optimize it, so i make a static member instance instead of using static func of Regex; static Regex filter= new Regex(".*0\\.000000.*\n", RegexOptions.Compiled); And use the static instance to replace the string....
15
50213
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
2
1366
by: =?Utf-8?B?TWFya19C?= | last post by:
I am trying to replace any occurance of minus-point "-." with minus-zero-point "-0." My regex: Regex repoint_num = new Regex(@"\-\."); My data: string xyz = "-.9"; My command: repoint_num.Replace(expr, "0."); The match is detected but the replace does not occur.
0
8236
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
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8335
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
8475
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
7159
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
6110
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
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.