473,408 Members | 1,854 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,408 software developers and data experts.

Regex - how to match until something

How do you replace an occurence until a certain point: You can see my
example code below and the output:
$string1 = 'Hello world today';
$string2 = 'Hello blahblah';

$string1 = preg_replace ( '/Hello.*(?!today)/', 'Hello ***' , $string1
);
$string2 = preg_replace ( '/Hello.*(?!today)/', 'Hello ***' , $string2
);

echo "$string1 - This should be Hello *** today\n";
echo "$string2 - This is correct\n";

Outputs:
======
Hello *** - This should be Hello *** today
Hello *** - This is correct

Apr 29 '06 #1
3 4519
Rik
ve*****@yahoo.com wrote:
$string1 = preg_replace ( '/Hello.*(?!today)/', 'Hello ***' , $string1 $string1 = 'Hello world today';


'Hello world today' isn't followed by "today", so it returns true,

Read up on regexes.

Learn what (.*?) means.

$string1 = preg_replace ( '/Hello(.*?)( today)/', 'Hello ***\2' , $string1);

Grtz,
--
Rik Wasmus
Apr 30 '06 #2
On Sat, 29 Apr 2006 13:46:31 -0700, veg_all wrote:
How do you replace an occurence until a certain point: You can see my
example code below and the output:


Would something like this:

$string1 = preg_replace ( '/Hello.*(today)/', 'Hello *** $1' , $string1);

be what you're looking for?
--
http://www.mgogala.com

Apr 30 '06 #3
On Sun, 30 Apr 2006 02:55:54 +0200, Rik wrote:
Read up on regexes.

Learn what (.*?) means.

$string1 = preg_replace ( '/Hello(.*?)( today)/', 'Hello ***\2' , $string1);

Grtz,

Actually, there is no need to put (.*) within the parenthesis as you are
not using it later. You are just creating another variable needlessly. In
addition to that, $2 is preferred to \2. The "backslash" variables are
only needed in the original string, not in the replacement. Also, no need
to use non-hungry version of .* as there is only one terminator ("today")
in the string. Invocation like

$string1 = preg_replace ( '/Hello.*(today)/', 'Hello *** $1' ,$string1);

would be a bit faster and use less memory.

--
http://www.mgogala.com

Apr 30 '06 #4

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

Similar topics

4
by: Masahiro Ito | last post by:
I have attached a block of text similar to the type that I am working with. I have been learning a lot about Regex - it is quite impressive. I can easily capture bits of info, but I keep having...
2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
2
by: Daniel Billingsley | last post by:
First, if MSFT is listening I'll say IMO the MSDN material is sorely lacking in this area... it's just a whole bunch of information thrown at you and you're left to yourself as to organizing it in...
15
by: Kay Schluehr | last post by:
I have a list of strings ls = and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in . There might be...
1
by: JM | last post by:
Hi, I am pretty new to regex, and I am trying to find a why to do: 1) I have a paragraph "... the black monkey (gamma5) is the one who develop the first inmunoresitance to alpha-23 also...
7
by: MrNobody | last post by:
I'm trying to do some regex in C# but for some reason linebreaks are causing my regex to not work. the test string goes like this: string ss = "<tagname...
4
by: DSmith1974 | last post by:
Are lookarounds supported in the boost regex lib? In my VS6 project using boost 1.32.0 I can declare a regex as.. <code_snippet> std::wstring wstrFilename = L"01_BAR08"; boost::wregex...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
2
by: natebruneau | last post by:
I wrote this regex <font>.*\((?<Inside>.*?)\)</font> "<font(Hi there)</font></font><font(Hi there)</font><font(Hi there)</font><font(Hi there)</font><font(Hi there)</font>" However it only...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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,...
0
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...

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.