0

in asp.net C#

let's say i want to store only the word "Batu Pahat" to database from following sentence

Conditions for Batu Pahat, MY at 11:00 am MYT

or "Mersing" from the following sentence?

Conditions for Mersing, MY at 12:00 pm MYT

Any one knows any algoritham to get this text? please help :)

UPDATE

Example sentences

Conditions for Kangar, MY at 10:00 am MYT
Conditions for Batu Pahat, MY at 11:00 am MYT
Conditions for Mersing, MY at 11:30 am MYT
Conditions for Segamat, MY at 12:00 pm MYT
Conditions for Johor Bahru Perdana, MY at 11:00 am MYT
Conditions for Muar, MY at 03:00 am MYT

Extra Info

The time "11:00 am" is not fixed.

4

2 に答える 2

5

または、正規表現を使用せずに(他の回答の代替として)、構造が固定されている場合のみ。

string startIgnorePart = "Conditions for ";
string findBefore = ",";
string original = "Conditions for Mersing, MY at 11:00 am MYT";
string extracted = original.Substring(startIgnorePart.Length, original.IndexOf(findBefore) - startIgnorePart.Length);
Console.WriteLine(extracted);
于 2013-01-17T04:02:26.033 に答える
3

正規表現はあなたを助けることができるはずです。私はこれをテストしていませんが、このようなものはうまくいくはずです。

Regex regex = new Regex("Conditions for (.*),");
var v = regex.Match("Conditions for Kangar, MY at 11:00 am MYT");
于 2013-01-17T04:01:04.587 に答える