473,215 Members | 1,242 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,215 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 2033
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.