To save typing, we'll assume:
RegexOptions opts = RegexOptions.Compiled | RegexOptions.IgnoreCase;
The line:
sText = sText.Replace(/"/g,"")
should be:
Regex Quotes = new Regex("\"", opts); // matches all quotes
sText = Quotes.Replace(sText, ""); // replace quotes with nothing (empty
string).
The block starting with:
if (sText.search(/(formsof|near|isabout)/i) == -1)
should be
Regex foo = new Regex("(formsof|near|isabout)", opts);
Regex andnot = new Regex(" (and not|and) ", opts);
Regex ornot = new Regex(" (or not|or) ", opts);
if(! foo.IsMatch(sText) ) {
sText = andnot.Replace(sText, new MatchEvaluator(TheMatch));
sText = ornot.Replace(sText, new MatchEvaluator(TheMatch));
sText = "" + sText + ""; // i have no idea what this line is
supposed to be for.
}
Elsewhere in the class that's doing this, you'll need:
private string TheMatch(Match m) {
return m.ToString();
}
For more information on .NET regular expressions, see:
http://msdn.microsoft.com/library/de...classtopic.asp
In general, though, you create a Regex object to hold the pattern, and then
you call methods on it to see if stuff matches the pattern or to perform
replacements. Match and replace operations that were one step in Perl or
JavaScript are thus two steps in C#. Sort of a bummer. So,
$mystr =~ m/whatever/;
is equivalent to
Regex R = new Regex("whatever");
R.IsMatch(mystr) f
and
$mystr =~ s/whatever/somethingelse/;
is equivalent to
Regex R = new Regex("whatever");
R.Replace(mystr, "somethingelse");
On the whole, I prefer the terser Perl-style syntax, but what are you gonna
do. I guess if you really hate having to create a regex object and then use
it, you could always derive your own string type in order to add .IsMatch
and .Replace methods to it, thus hiding all the actual regexp messiness from
the caller. But that seems like more trouble than it's probably worth.
"Jon Maz" <jo****@surfeuNOSPAM.de> wrote in message
news:uW**************@TK2MSFTNGP15.phx.gbl...
Hi All,
Am getting frustrated trying to port the following (pretty simple)
function
to CSharp. The problem is that I'm lousy at Regular Expressions....
//from http://support.microsoft.com/default...b;EN-US;246800
function fxnParseIt()
{
var sInputString = 'asp and database';
sText = sInputString;
sText = sText.replace(/"/g,"");
if (sText.search(/(formsof|near|isabout)/i) == -1)
{
sText = sText.replace(/ (and not|and) /gi,'" $1 "');
sText = sText.replace(/ (or not|or) /gi,'" $1 "');
sText = '"' + sText + '"';
}
sInputString = sText;
}
If anyone can give me a hand, it would be much appreciated!
TIA,
JON