Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void GetAgreementScope(string contents, System.IO.FileInfo file)
        {
            var regex = new Regex(@".*shall inure to all Bank Affiliates.*");
            var texts = contents.Split('.');
            foreach (var t in texts)
            {
                if (regex.IsMatch(t))
                {
                    var AScope = (t.Trim());
                    this.AgreementScope = Regex.Replace(AScope, @"\d", "");
                }
            }
        }



i want code to be something like

search "shall inure to all Bank Affiliates" from a text file when found return text else return a new field bool = false .

For eg

search shall inure to all Bank Affiliates.
if found AgreementScope = the whole text containg this set of words
else
rights = false ; boolean value



pls pls help ...
Posted
Comments
OriginalGriff 10-Dec-13 8:31am    
Answer updated
BillWoodruff 10-Dec-13 9:25am    
By the way, it might interest you to read this StackOverFlow thread on the use of the backslash-d in RegEx:

http://stackoverflow.com/questions/16621738/d-is-less-efficient-than-0-9

What nobody has pointed out is a list of a few things wrong with your idea:

1) You can't have two different return types from a single method. Either you're returning a string or a bool, not both. Instead, you can return a null or empty string if the text is not found.

2) If your search string is found, you can return the text it was found in, but what determines the "text" boundaries you're going to return?? Is it the whole file contents or what?

3) You called your method a Getsomething, but you don't return anything from it. That's not what I would expect the behavior of that method to be. "Get" method should always return something.
 
Share this answer
 
Comments
Member 10378420 10-Dec-13 7:44am    
Well i totaaly agree we can't have two different return types from a single method.....

please help me on how to return a null or empty string if the text is not found in same code
Dave Kreskowiak 10-Dec-13 8:37am    
Seriously?? You can't figure out how to return a string from a method?? You really need to pickup a beginners book on C# and work through it.

OriginalGriff already posted the code in his answer.
Why not just use string.Contains?
C#
if (t.Contains("shall inure to all Bank Affiliates."))
   {
   ...
   }
Or am I missing something?


[edit]
Try this:
C#
private string GetAgreementScope(string contents, System.IO.FileInfo file)
        {
            var texts = contents.Split('.');
            foreach (var t in texts)
            {
                if (t.Contains("shall inure to all Bank Affiliates."))
                {
                    return t;
                }
            }
        return null;
        }
[/edit]
 
Share this answer
 
v2
Comments
AmitGajjar 10-Dec-13 7:43am    
OP needs return value false in case of non existence of string and in case of existing in text file he want to return that string. I guess so :)
OriginalGriff 10-Dec-13 8:30am    
Answer updated.
Freak30 11-Dec-13 6:28am    
If you Split the file content by '.', I doubt you will find a text including the '.' in any of the array rows.
OriginalGriff 11-Dec-13 6:52am    
:OMissed that! :thumbsup:
If you want find text from file

C#
bool IsTextMatch(string text)
   {
       List<string> lines = new List<string>(File.ReadAllLines("file"));
       //Option 1
       //int lineIndex = lines.FindIndex(line => line.StartsWith(text));
       //Option 2
       int lineIndex = lines.FindIndex(line => line.Contains(text));
       if (lineIndex != -1)
       {
           return true;//Yes it matched
       }
       else return false;
   }
 
Share this answer
 
How about this..

C#
private bool TryGetAgreementScope(string contents, System.IO.FileInfo file, out string agreementScope)
        {
            var regex = new Regex(@".*shall inure to all Bank Affiliates.*");
            var texts = contents.Split('.');
            agreementScope = String.Empty;
            foreach (var t in texts)
            {
                if (regex.IsMatch(t))
                {
                    var AScope = (t.Trim());
                    agreementScope = Regex.Replace(AScope, @"\d", "");
                    return true;
                }
            }

            return false
        }


And you will use this function...

C#
string returnAgreement = String.Empty;

if(TryGetAgreementScope(string contents, file, out returnAgreement  ))
{
     Console.WriteLine(returnAgreement);
}
else
{
     Console.WriteLine("Not Found");
}
 
Share this answer
 
In your code, you are not reading the file, and, as Dave K. points out, you are not specifying exactly what you need to return if there is a match. What if there are multiple matches ?

However, there are ways to "package" two (or more) types of values/objects and return them from a Method.

For example, you could use a KeyValuePair to return two items of different Types:
C#
private KeyValuePair<bool, string> parseResult(string searchString, string contentString)
{
    return (contentString.Contains(searchString))
        ? new KeyValuePair<bool, string> (true, contentString)
        : new KeyValuePair<bool, string>(false, String.Empty);
}
Which you might use like this:
C#
var searchResult = parseResult("yes", "no and yes.");

if (searchResult.Key)
{
   // match found
}
else
{
   // match not found
}
You could also use the Tuple Class to return two (or more) items of different Types:
C#
private Tuple<bool, string> parseResult2(string searchString, string contentString)
{
    return (contentString.Contains(searchString))
        ? new Tuple<bool, string>(true, contentString)
        : new Tuple<bool, string>(false, String.Empty);
}
And, use it like this:
C#
var searchResult2 = parseResult2("yes", "no and yes.");

if (searchResult2.Item1)
{
    // match found
}
else
{
    // match not found
}
There's nothing unusual about packaging multiple Types into a return of a method: you'll not the TryParse methods kind-of do this by demanding you pass in a variable using 'ref which is then modified to hold the content if there's a match.

And, of course, you could define a Class, or Struct, with as many Fields/Types as you wish, and have your analytic Method return an instance of the Class, or Struct. In this response I decided to use KeyValuePair, and Tuple, examples for ... variety.
 
Share this answer
 
well thank u guys for so much help ..


to give u the clarity on what i want ..

i want my program to search for the "" words,
pull the whole line there
and display it in a new text file as text under a field


but if at all its unable to find that text it will create another field in the same output text file , that would be a boolean value , yes or no .....i.e. if the match is null or empty.

Thanks ..
I am still stuck ..
 
Share this answer
 
Try This:
C#
private bool GetAgreementScope(string contents, System.IO.FileInfo file)
{
      var regex = new Regex(@".*shall inure to all Bank Affiliates.*");
      var texts = contents.Split('.');
      foreach (var t in texts)
      {
          if (regex.IsMatch(t))
          {
              var AScope = (t.Trim());
              this.AgreementScope = Regex.Replace(AScope, @"\d", ""); 
              // the required line is stored in AgreementScope 
              // Assuming there are other function to write it to new file.
              return true;
          }else{
              return false;
          }
      }
}


Hope that helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900