473,474 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2055
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.