Regular Expression Help

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I am a newbie to C# and would appreciate any help converting the
following javascript function to C#

function fxnParseIt() {
// Note: sInputString code for demo purposes only, and should be
// replaced with user's code for getting in string value.

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;
}

Many Thanks
 
For those who aren't familiar with javascript, it would probably help if you
had pseudocode for what the function is trying to do.
 
Thanks

Replace all double quotes (clears the text and any improper quotations)
If the text string contains one of the key words "NEAR", "FORMSOF",
or
"ISABOUT", the parsing is complete
Else
Surround any instances of 'and' or 'and not' with quotes
Surround any instances of 'or' or 'or not' with quotes
Surround the entire string with quotes
 
Sorry here is the algorithm

Replace all double quotes (clears the text and any improper quotations)
If the text string contains one of the key words "NEAR", "FORMSOF", or
"ISABOUT", the parsing is complete
Else
Surround any instances of 'and' or 'and not' with quotes
Surround any instances of 'or' or 'or not' with quotes
Surround the entire string with quotes
 
Something like the following should help:

You'll need to add the following namespace:

using System.Text.RegularExpressions;

string InputString = "asp\" and database or not something else or something else and not something else";
Console.WriteLine("is=[" + InputString + "]");
InputString = InputString.Replace("\"","");
Console.WriteLine("is=[" + InputString + "]");
if (!Regex.Match(InputString, "(formsof|near|isabout)+").Success)
{
string pattern="(and not|and|or not|or)+";
string result= "'" + Regex.Replace(InputString,pattern,"'$1'") + "'";
Console.WriteLine("result=[" + result + "]");
}
return result;

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top