tags:

views:

140

answers:

5

My input string is:

"-->


Job Title: Test text
JobId: 56565-116503

City: San Diego
State: CA
Zip Code: 92108


Description: We are recruiting for a Controller to oversee all accounting and finance for a growing manufacturing company. We are looking for someone who is hands on full cycle accounting.


http://www.testurl.comhttp://test.xmlhttp://test.html-->"

I need to extract following string using C#/Regular expressions:

1."We are recruiting for a Controller to oversee all accounting and finance for a growing manufacturing company. We are looking for someone who is hands on full cycle accounting."

Can I please get help with the code?

Thanks for reading.

A: 

You can use something like this:

 JobId: [^\s]+\sCity: .*?\sState: [A-Z]{2}\sZip Code: [^\s]+\sDescription:
JG
A: 

Updated regex:

Regex.Replace(input, @"JobId:.*?City:.*?State:.*?Zip\s+Code:.*?(?=Description:)","");
Philippe Leybaert
Thanks for ur answer...i've modified my questions.Appreciate if you could reply to that as well.
Ed
+1  A: 
string pattern = @"JobId:.*?City:.*?State:.*?Zip\sCode:\s\d+\s";
string result = Regex.Replace(input, pattern, "");

Result: Senior Accountant - Growing Public Company Description: Multi-faceted multi-national corporation...

EDIT: comment based on your edit.

The above regex should handle multiple Description items. Example:

string input = "Senior Accountant - Growing Public Company JobId: 34441-345877 City: Santa Diego State: CA Zip Code: 93117 Description: 1st description here Description: Multi-faceted multi-national corporation...";

Replacement result is: "Senior Accountant - Growing Public Company Description: 1st description here Description: Multi-faceted multi-national corporation..."

Ahmad Mageed
Ed: I believe the above regex handles multiple descriptions, per your recent edit. I updated with a sample input string and the result.
Ahmad Mageed
A: 

If all you need is to extract the Description field values, perhaps replacing all the unwanted stuff with empty strings is not the best solution.

Assuming your question can be turned around to ask, "how do I extract the Description field values?", the regular expression that you need can be as simple as:

string pattern = @" Description: (.+)$";

Then you can use a Regex.Match/NextMatch loop to extract all descriptions.

Ates Goral
A: 

IF all of your descriptions separate the address from the description with "Description:", then you could go old-school:

string separator = "Description:";
int startIndex = originalText.IndexOf(separator)+separator.Length;
string newText = originalText.Substring(startIndex);
ebpower