467,075 Members | 982 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

REGEX.Replace Question

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
String, ByVal strReplace As String)

Return Regex.Replace(strIn, strFind, "\t")

End Function

I re-wrote it in C# and it works fine

private static string replaceAll(string strIn, string strFind, string
strReplace)
{
return Regex.Replace(strIn, strFind, "\t");
}

(I do not want to re-write the whoel app in C# so don't suggest that to me!!)

Please tell me I am an idiot and I am missing something huge.

--
Thanks
Andrew
Nov 21 '05 #1
  • viewed: 3968
Share:
9 Replies
Doesn't "\t" in C# = vbtab in VB?

so try Return Regex.Replace(strIn, strFind, vbtab)

Chris
"Whitless" <Wh******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
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
String, ByVal strReplace As String)

Return Regex.Replace(strIn, strFind, "\t")

End Function

I re-wrote it in C# and it works fine

private static string replaceAll(string strIn, string strFind, string
strReplace)
{
return Regex.Replace(strIn, strFind, "\t");
}

(I do not want to re-write the whoel app in C# so don't suggest that to
me!!)

Please tell me I am an idiot and I am missing something huge.

--
Thanks
Andrew

Nov 21 '05 #2
So how would incorporate that into a string for example I want to replace all
occureneces of "^Z" with "0\tZ" in other words all lines that start with Z to
start with 0(tab)Z. I do not want to hard code a tab I want to be able to
pass the function a regular expression to replace the found text

"Chris, Master of All Things Insignifican" wrote:
Doesn't "\t" in C# = vbtab in VB?

so try Return Regex.Replace(strIn, strFind, vbtab)

Chris
"Whitless" <Wh******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
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
String, ByVal strReplace As String)

Return Regex.Replace(strIn, strFind, "\t")

End Function

I re-wrote it in C# and it works fine

private static string replaceAll(string strIn, string strFind, string
strReplace)
{
return Regex.Replace(strIn, strFind, "\t");
}

(I do not want to re-write the whoel app in C# so don't suggest that to
me!!)

Please tell me I am an idiot and I am missing something huge.

--
Thanks
Andrew


Nov 21 '05 #3
I'm not that familiar with regular expressions, but I think this is what you
want.

Dim S as String
S = "0" & vbtab & "Z"
Return Regex.Replace(strIn, strFind, S)

Chris

"Whitless" <Wh******@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
So how would incorporate that into a string for example I want to replace
all
occureneces of "^Z" with "0\tZ" in other words all lines that start with Z
to
start with 0(tab)Z. I do not want to hard code a tab I want to be able to
pass the function a regular expression to replace the found text

"Chris, Master of All Things Insignifican" wrote:
Doesn't "\t" in C# = vbtab in VB?

so try Return Regex.Replace(strIn, strFind, vbtab)

Chris
"Whitless" <Wh******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
> 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
> String, ByVal strReplace As String)
>
> Return Regex.Replace(strIn, strFind, "\t")
>
> End Function
>
> I re-wrote it in C# and it works fine
>
> private static string replaceAll(string strIn, string strFind, string
> strReplace)
> {
> return Regex.Replace(strIn, strFind, "\t");
> }
>
> (I do not want to re-write the whoel app in C# so don't suggest that to
> me!!)
>
> Please tell me I am an idiot and I am missing something huge.
>
> --
> Thanks
> Andrew


Nov 21 '05 #4

I think this is a bug with VB.Net. You should report it to Microsoft.

Robby

"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:u2**************@TK2MSFTNGP10.phx.gbl...
I'm not that familiar with regular expressions, but I think this is what
you want.

Dim S as String
S = "0" & vbtab & "Z"
Return Regex.Replace(strIn, strFind, S)

Chris

"Whitless" <Wh******@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
So how would incorporate that into a string for example I want to replace
all
occureneces of "^Z" with "0\tZ" in other words all lines that start with
Z to
start with 0(tab)Z. I do not want to hard code a tab I want to be able
to
pass the function a regular expression to replace the found text

"Chris, Master of All Things Insignifican" wrote:
Doesn't "\t" in C# = vbtab in VB?

so try Return Regex.Replace(strIn, strFind, vbtab)

Chris
"Whitless" <Wh******@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
> 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
> String, ByVal strReplace As String)
>
> Return Regex.Replace(strIn, strFind, "\t")
>
> End Function
>
> I re-wrote it in C# and it works fine
>
> private static string replaceAll(string strIn, string strFind, string
> strReplace)
> {
> return Regex.Replace(strIn, strFind, "\t");
> }
>
> (I do not want to re-write the whoel app in C# so don't suggest that
> to
> me!!)
>
> Please tell me I am an idiot and I am missing something huge.
>
> --
> Thanks
> Andrew


Nov 21 '05 #5
"Robby" <ed****@not.my.email.com> schrieb:
I think this is a bug with VB.Net. You should report it to Microsoft.


This is not a bug. VB.NET simply doesn't support "\t" (et al.) inside
string literals. Use the 'ControlChars.*' constants instead.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #6


It IS a bug.

We are not talking about string literals. We are talking about Regular
Expression replace patterns. The Regular Expression pattern "\t" is a [tab].
So when you use "\t" in a Regular Expression replace pattern it should
replace it with a [tab]. A Regular Expression find pattern of "\t" finds
tabs in VB.Net. According to pattern syntax a replace pattern of "\t"
should insert [tab].

Robby

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eF**************@TK2MSFTNGP14.phx.gbl...
"Robby" <ed****@not.my.email.com> schrieb:
I think this is a bug with VB.Net. You should report it to Microsoft.


This is not a bug. VB.NET simply doesn't support "\t" (et al.) inside
string literals. Use the 'ControlChars.*' constants instead.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #7
On 2004-12-11, Robby <ed****@not.my.email.com> wrote:


It IS a bug.
No, it's not.

We are not talking about string literals. We are talking about Regular
Expression replace patterns.
Actually, we *are* talking about string literals. The second parameter
to the static Regex.Replace function is a regex pattern. But if you
look at the example again, the "\t" is in the third parameter, which is
NOT a regex pattern, it's a simple replacement string that allows only a
very few special characters, such as backreferences.

The Regular Expression pattern "\t" is a [tab].
So when you use "\t" in a Regular Expression replace pattern it should
replace it with a [tab]. A Regular Expression find pattern of "\t" finds
tabs in VB.Net.
Correct, but...
According to pattern syntax a replace pattern of "\t"
should insert [tab].


There's no such thing as a "replace pattern", at least not in the sense
that it's a regex pattern . What would that mean anyway? If you think
about it, you'll realize that there's no way the replacement string can
be a regex pattern.

e.g., what would you return for

Regex.Replace("1234", "123", "\w+\t\d+")
Nov 21 '05 #8


Doh ... it is the third argument. Silly me.

Robby
"David" <df*****@woofix.local.dom> wrote in message
news:slrncrkori.5gi.df*****@woofix.local.dom...
On 2004-12-11, Robby <ed****@not.my.email.com> wrote:


It IS a bug.


No, it's not.

We are not talking about string literals. We are talking about Regular
Expression replace patterns.


Actually, we *are* talking about string literals. The second parameter
to the static Regex.Replace function is a regex pattern. But if you
look at the example again, the "\t" is in the third parameter, which is
NOT a regex pattern, it's a simple replacement string that allows only a
very few special characters, such as backreferences.

The Regular Expression pattern "\t" is a [tab].
So when you use "\t" in a Regular Expression replace pattern it should
replace it with a [tab]. A Regular Expression find pattern of "\t" finds
tabs in VB.Net.


Correct, but...
According to pattern syntax a replace pattern of "\t"
should insert [tab].


There's no such thing as a "replace pattern", at least not in the sense
that it's a regex pattern . What would that mean anyway? If you think
about it, you'll realize that there's no way the replacement string can
be a regex pattern.

e.g., what would you return for

Regex.Replace("1234", "123", "\w+\t\d+")

Nov 21 '05 #9
What I don't get though is (as my original post highlighted) I could pass the
third argument "\t" in C# and it would return a tab. I realise that there
would not be much need for a regex replace pattern, however it can be done in
C# for my example. Why?

"David" wrote:
On 2004-12-11, Robby <ed****@not.my.email.com> wrote:


It IS a bug.


No, it's not.

We are not talking about string literals. We are talking about Regular
Expression replace patterns.


Actually, we *are* talking about string literals. The second parameter
to the static Regex.Replace function is a regex pattern. But if you
look at the example again, the "\t" is in the third parameter, which is
NOT a regex pattern, it's a simple replacement string that allows only a
very few special characters, such as backreferences.

The Regular Expression pattern "\t" is a [tab].
So when you use "\t" in a Regular Expression replace pattern it should
replace it with a [tab]. A Regular Expression find pattern of "\t" finds
tabs in VB.Net.


Correct, but...
According to pattern syntax a replace pattern of "\t"
should insert [tab].


There's no such thing as a "replace pattern", at least not in the sense
that it's a regex pattern . What would that mean anyway? If you think
about it, you'll realize that there's no way the replacement string can
be a regex pattern.

e.g., what would you return for

Regex.Replace("1234", "123", "\w+\t\d+")

Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Theo Chakkapark | last post: by
6 posts views Thread by tshad | last post: by
17 posts views Thread by clintonG | last post: by
6 posts views Thread by Martin Evans | last post: by
15 posts views Thread by morleyc@gmail.com | last post: by
reply views Thread by Karch | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.