473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Translating JavaScript function with Regex to CSharp

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

Jul 21 '05 #1
3 2040
Hi Taruntius,

Fantastic, thanks for your help! Below is the (almost identical) code that
I am now using!

Cheers,

JON

---------------------------------------------------------

public static string ParseFullTextIndexSearchTerm(string inputString)
{
RegexOptions opts = RegexOptions.Compiled | RegexOptions.IgnoreCase;

Regex Quotes = new Regex("\"", opts); // matches all quotes
outputString = Quotes.Replace(outputString, ""); // replace quotes with
nothing (empty string).

Regex keyWords = new Regex("(formsof|near|isabout)", opts);
Regex andnot = new Regex(" (and not|and) ", opts);
Regex ornot = new Regex(" (or not|or) ", opts);
if(! keyWords.IsMatch(outputString) )
{
outputString = andnot.Replace(outputString, new MatchEvaluator(TheMatch));
outputString = ornot.Replace(outputString, new MatchEvaluator(TheMatch));
outputString = "\"" + outputString + "\"";
}

return outputString;

}

public static string TheMatch(Match m)
{
return "\"" + m.ToString() + "\"";
}


Jul 21 '05 #2
Hi,

1.) I wouldn't recommend using RegexOptions.Compiled. It doesn't seem
that the OP is parsing thousands characters long strings, so the
RegexOptions.Compiled will almost certainly slow things down (it causes
a dynamic assembly to be generated, which is not what you should want at
all unless you really know you need it).

2.) You don't need to create the Regex objects, there are static methods
in the Regex class that do it for you:

3.) There is absolutely no need to use the match evaluator, you can use
substitutions ($1, $2, etc.) in the replacement string

So I would suggest rewriting the function as: (not tested)

public static string Parse(string s)
{
s = s.Replace("\"", ""); // no need for regex

if (!Regex.Match(s, "(formsof|near|isabout)",
RegexOptions.IgnoreCase).Success))
{
s = Regex.Replace(s, " (and not|and) ", "\" $1 \"",
RegexOptions.IgnoreCase);
s = Regex.Replace(s, " (or not|or) ", "\" $1 \"",
RegexOptions.IgnoreCase);
s = '"' + s + '"';
}

return s;
}

HTH,
Stefan
MS News (taruntius) wrote:
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



Jul 21 '05 #3
That's great Stefan, it works almost as posted, and it is much shorter!

Thanks!

JON
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Vishant | last post by:
Hi, I'm new to javascript and regEx and trying to solve the following problem. I have a function which validates the password if there is a number:...
4
by: Demetri | last post by:
Lets pretend you have a string array and each element is a sentence. Two of those elements read as follows: 1. The fox escaped from the hound. 2. The fox almost escaped. Now lets say you loop...
4
by: Jon Maz | last post by:
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...
4
by: gs | last post by:
I have searched Google, MSDN,... for a week. I am still unable to make available functions in my csharp dll as native windows functions for some legacy non dotnet application I just want to...
0
by: delta7 | last post by:
Hi, I'm now nearing the completion of my first c# development. I have two applications. The first is a simple database app that uses an Access database to store Regex strings along with a...
0
by: ajitgoel | last post by:
Hi; We have a Javascript function which I have been tasked to move to a CSharp class. This javascript function uses Regular expression extensively. This function has a string input parameter and...
6
by: Ajit Goel | last post by:
Hi; We have a Javascript function which I have been tasked to move to a CSharp class. This javascript function uses Regular expression extensively. This function has a string input parameter and...
1
by: Abhishek | last post by:
Hi I have tried one more javascript Validator Script to Validate the Money entered in to the TextBox. <script language='javascript'> function checkNumber(val,e){ if(window.event){ var strkeyIE...
5
by: Abhishek | last post by:
Hi this is my another validator in javascript to validate the Phone Number :-) <script language='javascript'> function funcCheckPhoneNumber(ctrtxtMobile,e){ if(window.event){ var strkeyIE =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.