I have small problem. I try to write a function to extract something in C# (I have to convert a regular expression from PHP to C#).
I generally write it like this:
public static class ExtensionMethods
{
public String PregReplace(this String input, string[] pattern, string[] replacements)
{
for (var i = 0; i < pattern.Length; i++)
{
input = Regex.Replace(input, pattern[i], replacements[i]);
}
return input;
}
}
But I have a problem with these examples (code in PHP)
preg_replace('@<head[^>]*?>.*?</head>@siu', ' ', $result);
preg_replace('@</?((frameset)|(frame)|(iframe))@iu', "\n\$0", $result);
But when I use this regulax expression in C#
String[] pattern = new String[2]{"@<head[^>]*?>.*?</head>@siu", "@</?((frameset)|(frame)|(iframe))@iu"};
String[] replace = new String[2]{" ", "\n\$0"};
input.PregReplace(pattern , replace ); //my new function
I don’t have any differences in input (I don’t catch a regular expression).
Can you help me with my function? Do I have error in regex?
EDIT:
I changed code to:
String[] pattern = new String[2]{"<head[^>]*?>.*?</head>", "</?((frameset)|(frame)|(iframe))"};
String[] replace = new String[2]{" ", "\n\\$0"};
input = "input = "><head><title>something</title></head><body>sdegsehgaeg<frame>aggsd</frame></";
string s = input.PregReplace(pattern, replace);
In answer I get
> <body>sdegsehgaeg
\<frame>aggsd
\</frame></
It is
> <body>sdegsehgaeg\n\\<frame>aggsd\n\\</frame></
This is only to much char \ for \n\$0. If I change \n\$0 to \n\$0 I get error (urecognized escape sequence)
Ok I solved problem (\n$0)
Thank you for help.
Remove the
@in your code, and thesiuandiuafter your last@. In php you need those modifiers, in C# you would call someRegexOptionsif necessary.