Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to read a text file and extract a clause/ sentence containg specific words from the file .


C#
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern =@"(?<=^|\.)[^.]*?(? =\bshall incure to all bank of amrerica Affiliates\b).*(\.|$)"
      string input = "anita singh is good girl.Supplier expressly acknowledges and agrees that the rights and obligations of Bank of india set forth in this Agreement shall inure to all Bank of india Affiliates.anita is a good girl." ;
      Match match = Regex.Match(input, pattern);
      if (match.Success)
      {
         Console.WriteLine("Match: " + match.Value);
      } 
   }
}



output would be


Supplier expressly acknowledges and agrees that the rights and obligations of Bank of India set forth in this Agreement shall inure to all Bank of india Affiliates.
Posted
Comments
ZurdoDev 21-Nov-13 7:43am    
Where are you stuck?
BillWoodruff 21-Nov-13 11:51am    
shall incure to all bank of amrerica Affiliates
shall inure to all Bank of india Affiliates

The fact that your search string doesn't have a match indicates to me you have never run this code. Homework ?
Member 10378420 22-Nov-13 2:29am    
sorry its india insted of america

I found this as a possible solution. I know I'm keeping it around from now on.

C#
public static bool ExactMatch(string input, string match)
{
    return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)));
}
 
Share this answer
 
v2
Comments
BillWoodruff 21-Nov-13 11:52am    
+5 Nice !
bowlturner 21-Nov-13 13:07pm    
Thanks!
Member 10378420 22-Nov-13 2:31am    
thanks trying to ckeck if it works
Make your life easier and split the string into sentences first.

Example:
C#
var regex = new Regex(@".*your text goes here.*");
var text = "This is a Test 2. This is the sentence with 'your text goes here' xyz. The middle sentence is the one we want.";

var texts = text.Split('.');
foreach(var t in texts) {
   if(regex.IsMatch(t)) {
      Console.WriteLine(t.Trim());
   }
}
 
Share this answer
 
Comments
Member 10378420 22-Nov-13 3:16am    
Thanks the code works fine but what if there is text file called contract.txt and somewhere in the text file "your text goes here" is written .

How to extract it ?
Nicholas Marty 22-Nov-13 3:37am    
You just read the file into a string. Either line by line or the complete file at once. This pretty much depends what you want to use it for. If your file isn't that big reading the whole file into memory shouldn't be an issue.
Member 10378420 27-Nov-13 4:39am    
HI Marty thanks for the help . i m grateful to u ...


my code runs perfectly ..with a slight problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var regex = new Regex(@".*shall inure to all Bank Affiliates.*");
var text = "If Supplier does not respond promptly to any request by Bank for telephone consultative service, then Bank may attempt to contact the next more responsible or qualified person on the Calling List until contact is made and a designated person responds to the call.2.6 Supplier expressly acknowledges and agrees that the rights and obligations of Bank set forth in this Agreement shall inure to all Bank Affiliates. In the event this Agreement Proprietary to Bank .";
var texts = text.Split('.');
foreach(var t in texts)
{
if(regex.IsMatch(t))
{
Console.WriteLine(t.Trim());
}
}
}
}
}



output is

6 Supplier expressly acknowledges and agrees that the rights and obligations of Bank set forth in this Agreement shall inure to all Bank Affiliates

I dont want 6 to be coming in the output ...plsssss help
Member 11503141 7-Aug-15 8:39am    
gg
Member 10378420 27-Nov-13 4:39am    
anybody plsssss

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