473,651 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex replace help

Hi,

I have a string like "bla<cut>blubb< cut>bla<cut>blu bb" and want to replace
the substring from the first occurrence of <cut> to the second with "TEST"
in order to get "blaTESTbla<cut >blubb".
So far I only managed to get blaTESTblubb:

Regex iRegex = new Regex("<cut>.*< cut>");
return iRegex.Replace( "bla<cut>blubb< cut>bla<cut>blu bb", "TEST");
Mar 28 '06 #1
4 2192


Julius Fuchs wrote:

Regex iRegex = new Regex("<cut>.*< cut>");


You want non-greedy matching I think
Regex iRegex = new Regex("<cut>.*? <cut>");
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 28 '06 #2
Martin Honnen wrote:
You want non-greedy matching I think
Regex iRegex = new Regex("<cut>.*? <cut>");


Thanks for your answer but that's not exactly what I wanted. Imagine the
following string: "a<cut>b<cut>c< cut>d<cut>e", with "<cut>.*?<c ut>" as
pattern I would get "aTESTcTEST e" instead of "aTESTc<cut>d<c ut>e" what I
want.
Mar 28 '06 #3

The lazy (non-greedy) match indicated by Martin works but the lazy
matches are generally much slower than that standard (greedy) matches.

If performance is an issue you might look at

Regex iRegex = new Regex("<cut>\b\ w*\b<cut>");

The \b (word boundary) prevents matching across multiple <cut> tags
NOTE: This will not find empty pairs (e.g. in bla<cut><cut>dd sd<cut>,
it will not find the <cut><cut>)

or

Regex iRegex = new Regex("<cut>((? !<cut>).)*<cut> ")

where the negative lookahead prevents us passing the middle <cut>.
NOTE: This will find the empty pair

hth,
Alan

Mar 28 '06 #4

Use

return iRegex.Replace( "a<cut>b<cut>c< cut>d<cut>e", "TEST", 1);

This will return the string with only the first match replaced.

aTESTc<cut>d<cu t>e

hth,
Alan.

Mar 28 '06 #5

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

Similar topics

3
2076
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';
6
1770
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone") %>',"(\d{3})(\d{3})(\d{4})","($1) $2-$3") runat="server"/> I have this inside my Repeater and want it to filter the field during bind. I can do it afterwards by just looping through the repeater items, but that is extra work and time.
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/
4
3838
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
2
1288
by: Art | last post by:
Hi & help, I'm trying to parse arithmetic expressions such as 4*(7-2.1). The first thing I'm trying is to add spaces to get: 4 * ( 7 - 2.1 ). I thought that Regular Expressions would be the way to go. I tried: regex.replace("\D", " \D ") which did not work. I don't know how to refer to the string that was matched in forming the replacement.
2
2362
by: Alex Maghen | last post by:
This is a bit of an abuse of this group. Just a nit, but I'm hoping someone really good with Regular Expressions can help me out here. I need to write a regular expression that will do the following: Search a whole blob of text (including newlines and everything). Find any text enclosed in brackets (), and replace it with a string I provide and then concatenate the text that had been enclosed in the brakets, without the brakets.
6
9662
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other words "this is a .5k example" goes to "this is a 500 example" The replace should not touch '.5k' that occurs inside a word. For example:
15
50217
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
1
12175
by: jonnyboy6969 | last post by:
Hi All Really hoping someone can help me out here with my deficient regex skills :) I have a function which takes a string of HTML and replaces a term (word or phrase) with a link. The pupose is that I seek out terms which are in a glossary on our site, and automatically link to this definition. Its slightly complex becase certain elements have to be ignored, for exampleI dont want to add links within existing links, or for example link...
0
1728
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming operation by a long-shot is the regex.Replace. Basically the only purpose of it is to remove spaces between opening/closing tags and the element name. Surely there is a better way. private string FixupJavascript(string htmlCode) { string result...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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
8466
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
8584
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
7299
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
6158
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
5615
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.