Regular Expression from Perl

  • Thread starter Thread starter rt
  • Start date Start date
R

rt

Hello,

I'm having fits trying to make a regular expression work like I've used in
Perl.

Example:

input: "ABCDE"
pattern: "(.*)"
replacement: "This is $1"

A call to Regex.Replace(input, pattern, replacement) returns "This is
ABCDEThis is"

I don't understand where the trailing "This is" is coming from. What I
expected was "This is ABCDE".

Can anyone shed some light?

Thanks,

Rick
 
Try something as :

Regex reg = new Regex("(InputText)");
Console.Out.WriteLine(reg.Replace("This is InputText","ABCDE",1));
 
Thanks for the reply but I should have elaborated. This works for one
parituclar case.
I have users entering both the pattern and replacement so I can't use the
Replace you suggest. What I don't understand is why the syntax (.*) doesn't
replace the way I was expecting. What is it about "(.*)" as the only thing
in the patten that causes the problem?

For example with the following:
input "ABCD12345"
pattern "((ABCD)(.*))"
Replacement is "First: $2 Second: $3"

The result is "First: ABCD Second: 12345"

Exactly as expected.

Changing the pattern to "(.*)" and Replacement to "First: $1" result in
"First: ABCD12345First:"

Given the user could enter either of the above I don't want to have to parse
the "stuff" before just to determine the type of Replace to call. Worse, I
don't see where the replace was consistant so 1) either there is bug, 2) I
don't understand how to use Replace or 3) I have to analyze the
pattern/Replacement before to determine which Replace to call (yuck).

Thanks,

Rick
 
In this case, i'm not sure the Regex is the best solution.

The best way, i think, is to use String.Format :

string test = "My first word is {0}, my second word is {1}";
string result = String.Format(test,"word1", "word2");
Console.Out.WriteLine(result);

Output : My first word is word1, my second word is word2
 
Not to be rude but I see this is just going to go in a circle. This is an
application that we've upgrade from java to .NET. That type of change, which
wouldn't work for many cases, would cause changes at dozens of facilities
and thousands of entries. I know you didn't know this but your replies seem
to not understand the basic problem which is the use of "(.*)". Maybe I
wasn't clear.

I was able to get around the problem, for now, by using "(.+)" instead of
"(.*)" for the pattern whenever the user enters "*" or ".*". Easy enough to
check. We aways have at least one character so this works.

I still think there is a bug in RegEx or I don't understand the scope of "*"
in the .NET RegEx.

Thanks,

Rick
 
Hi Rick,

I am checking to see if there is any known issue about this problem and I
will update you with my information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
I don't found an equivalence in .NET.


rt said:
Not to be rude but I see this is just going to go in a circle. This is an
application that we've upgrade from java to .NET. That type of change, which
wouldn't work for many cases, would cause changes at dozens of facilities
and thousands of entries. I know you didn't know this but your replies seem
to not understand the basic problem which is the use of "(.*)". Maybe I
wasn't clear.

I was able to get around the problem, for now, by using "(.+)" instead of
"(.*)" for the pattern whenever the user enters "*" or ".*". Easy enough to
check. We aways have at least one character so this works.

I still think there is a bug in RegEx or I don't understand the scope of "*"
in the .NET RegEx.

Thanks,

Rick

Worse, 2) What
 
Back
Top