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

regular expression

Please could somebody tell me the regular expression in thefollowing case.

I want to replace variable within {} with some other variable in the string.

for e.g:-
$string = "{she} is my friend";
$replace_variable = "sita";
how to replace "{she}" with "sita" using regular expression?
Oct 31 '06 #1
9 1168
ronverdonk
4,258 Expert 4TB
Why would you use a regular expression when the following will do and faster?
[php]str_replace('{she}', $replace_variable, $string);[/php]
Ronald :cool:
Oct 31 '06 #2
Can you please tell me how to use regular expression in this case as i am asked
to do using it?
Oct 31 '06 #3
ronverdonk
4,258 Expert 4TB
Not very much different[php]preg_replace('/{she}/', $replace_variable, $string);[/php]
Ronald :cool:
Oct 31 '06 #4
thanx a lot for the quick reply :)
Oct 31 '06 #5
Can you also tell how to replace every thing in the string that starts and ends with curly braces with an array?

for e.g:
$string = " {this} chocolate belongs to {that} boy";
$replace_variable = array('that', 'this');

It will be of great help if you could reply soon.
Oct 31 '06 #6
ronverdonk
4,258 Expert 4TB
YOU HAVE DOUBLE POSTED THIS QUESTION IN THE SAME THREAD!!! DO NOT DO THAT!! READ THE POSTING GUIDELINES BEFORE YOU CONTINUE.
Well I certainly do not know using a regular expression. That would be a large expression! Maybe someone else can help you here.

All I know is how to replace, using ARRAYS, strings in the str_replace as in the following example:
[php]// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);[/php]
Ronald :cool:
Oct 31 '06 #7
This will helpful for replacing strings in the braces.

<?php

$string = " {this} chocolate belongs to {that} boy";
$pattern = "/{([^}]+)}/i";
preg_match_all($pattern,$string,$result);

$values = $result[1];
$replace_variable = array('rep1', 'rep2');
$newphrase = str_replace($values, $replace_variable, $string);
echo "<br> String : ".$string;
echo "<br> New String : ".$newphrase;

?>
Nov 3 '06 #8
ronverdonk
4,258 Expert 4TB
I think the original questionn was how to replace the entire {string}, including braces, not just the value within the braces. This sort of replacement is usually done in most templates. So {this} must be replaced by rep1.

Ronald :cool:
Nov 3 '06 #9
This will replace the entire string,including the braces.

$string = " {this} chocolate belongs to {that} boy";
$pattern = "/({[^}]+})/i";
preg_match_all($pattern,$string,$result);

$values = $result[1];
$replace_variable = array('rep1', 'rep2');
$newphrase = str_replace($values, $replace_variable, $string);
echo "<br> String : ".$string;
echo "<br> New String : ".$newphrase;

Ranjith
Nov 4 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
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.