473,399 Members | 3,832 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,399 software developers and data experts.

Issue with "RegularExpressions.Regex.Replace"

I have another question about Regular Expression. If I use:

if (temp.Contains("Ending") == true)
{
temp =
System.Text.RegularExpressions.Regex.Replace(temp, "Ending",
"Beginning");
}

It seems that while "Ending" is replaced with "Beginning", it also
deletes the space after "Ending". For instance, "Period Ending July
31, 2007" is now "Period BeginningJuly 31, 2007" (Note: no space
betweem "Beginning" and "July" after Replace).

Any advice?

Aug 28 '07 #1
3 1905
Curious,

Not sure what you're doing wrong in your code but here is mine and it
works as expected. I'm using VS.NET 2005.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RegEx
{
class Program
{
static void Main(string[] args)
{
string test = "Period Ending July 31, 2007";
string test2 = Regex.Replace(test, "Ending", "Beginning");
// Test2 is now "Period Beginning July 31, 2007"
}
}
}

Also not that your temp.Contains(...) is not necessary. If
Regex.Replace(...) find it, it will replace the string. No need to
check for it.

Jason Newell
Software Engineer
www.jasonnewell.net

Curious wrote:
I have another question about Regular Expression. If I use:

if (temp.Contains("Ending") == true)
{
temp =
System.Text.RegularExpressions.Regex.Replace(temp, "Ending",
"Beginning");
}

It seems that while "Ending" is replaced with "Beginning", it also
deletes the space after "Ending". For instance, "Period Ending July
31, 2007" is now "Period BeginningJuly 31, 2007" (Note: no space
betweem "Beginning" and "July" after Replace).

Any advice?
Aug 28 '07 #2
Curious wrote:
I have another question about Regular Expression. If I use:

if (temp.Contains("Ending") == true)
{
temp =
System.Text.RegularExpressions.Regex.Replace(temp, "Ending",
"Beginning");
}

It seems that while "Ending" is replaced with "Beginning", it also
deletes the space after "Ending". For instance, "Period Ending July
31, 2007" is now "Period BeginningJuly 31, 2007" (Note: no space
betweem "Beginning" and "July" after Replace).

Any advice?
Use string.Replace instead. There is no reason to use Regex.Replace when
you don't have a regular expression.

If you _do_ have a regular expression, but have replaced it in the
example, show the actual code that you are using. It's very hard to find
errors in code that you can't see.

--
Göran Andersson
_____
http://www.guffa.com
Aug 28 '07 #3
Thanks all for the help!

Here's my situation: I have a PDF file containing multiple pages. I'll
need to replace the word for "Period Ending July 31, 2007" on every
single page. The replacement is successful on the pages except for a
few pages where the space is missing and the line is changed to
"Period BeginningJuly 31, 2007".

In the debugger, the word is always successfully replaced. However, it
shows the incorrect result only for a few pages. I think that this is
related to the way the original PDF file was created

Aug 28 '07 #4

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

Similar topics

6
by: vigi98 | last post by:
Hello all, Can someone confirme that this: var strURLpiece = "UK & Ireland"; strURLpiece.replace("&", "%26"); replaces all occurrence of the character & by %26 in strURLpiece, ie that...
5
by: Mark Johnson | last post by:
Regex("@("); brings an error (missing ")"). How do you serarch for a ( with Regex ? Mark Johnson, Berlin Germany mj10777@mj10777.de
6
by: Marty | last post by:
Hi, I would like to replace "\r\n" by "_" within a specific string. I tried : strMyString.Replace('\r', '_'); strMyString.Replace('\n', '_'); or...
4
by: Brent | last post by:
Take this string: "---------------------------------------- " (i.e., hyphens followed by a newline ) I thought I could match it simply with this Regex: "-*?\n"
2
by: Jeff Jarrell | last post by:
I want to use the regex.replace for a string containing "%s" I can't seem to get the "%s" escaped. I tried a normal "\%s" but that doesn't seem to do it. Picks up any "s"....
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
3
by: Efi | last post by:
Hello, I am trying to capture empty lines, for example : 1. ABCDEF 2. 3. abcdef I would like to capture line number 2. I am using the following regex - new...
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...

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.