Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
Input String : "Send comments on this rule to: U.S. Department of Transportation, Docket Operations, West Building Ground Floor, Room W12-140, 1200 New Jersey Ave. SE., Washington, DC 20590-0001; Telephone: 800-647-5697; Fax: 202-493-2009. You must identify the Docket Number FAA-2011-1211; Airspace"

I will input this string, i am having the numbers (800-647-5697,202-493-2009) already
i just want to know which is the closest word (telephone or fax) before or after the particular no.

Or we can input  numbers (800-647-5697,202-493-2009) directly to find the closest word (telephone or fax) before or after.


What I have tried:

I have tried Contains function but it doesn't give proper output for second number.
Posted
Updated 16-Jun-16 8:24am
Comments
Sergey Alexandrovich Kryukov 16-Jun-16 12:11pm    
"Closest" needs to be strictly defined. This is not as simple as it may seem. If you are talking of something like elastic search, this is a whole science. Regular expressions are not useful in such cases. But if you need exact match, what's the problem? If you want to identify one of the phones, just do it. It's not "closest", it would be a strict match.
—SA
BillWoodruff 16-Jun-16 12:23pm    
How variable is the data you want to parse ? Is it always the case that the words "Fax" and "Telephone" appear before the numbers ?
Nikhil Bhivgade 17-Jun-16 9:33am    
No it may be before or after some time 5-6 word in bettween them
ZurdoDev 16-Jun-16 13:59pm    
You need to parse the data, perhaps a simple split on spaces and then loop through. How are you getting the numbers currently?

1 solution

If you mean you have the input string and the number, and you want to find if "Telephone" or "Fax" preceeds it, then for the input you show a regex like this:
\w+(?=:\s800-647-5697)
will extract "Telephone" and this:
\w+(?=:\s202-493-2009)
will extract "Fax"
So with a preset format:
C#
string number = "202-493-2009";
string pattern = string.Format(@"\w+(?=:\s{0})", number);
Match m = Regex.Match(input, pattern);
if (m.Success)
   {
   Console.WriteLine(m.Value);
   }
You should get the word you want.
[edit]Missed teh final close bracket... :O[/edit]
 
Share this answer
 
v2
Comments
Nikhil Bhivgade 17-Jun-16 12:30pm    
The telephone number for the Public Reading Room is (202) 566-1744, and the fax number for the Air and Radiation Docket is (202) 566-1742.

If it is like above then how can i find... ?
OriginalGriff 17-Jun-16 12:36pm    
Sorry, but I don't understand what you are asking: Remember that I can;t see your screen, access your HDD, or read you mind - I only get exactly what you type, and have no context beyond that to know what you w=know - please try to explain without the assumed knowledge of you data and project you have and I don't!

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