REGEX.Replace Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Doesn't "\t" in C# = vbtab in VB?

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

Chris
 
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 said:
Doesn't "\t" in C# = vbtab in VB?

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

Chris
 
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 said:
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
 
Robby said:
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.
 
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
 
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+")
 
Doh ... it is the third argument. Silly me.

Robby


David said:
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+")
 
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 said:
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+")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top