Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi, please help me.
the key words is the following:
"At Apr 29 2011 13:12:45 UTC,happend an earthquake(near longitude 121.98,latitude 21.18) in TAIWAN REGION,the depth is about 177km."

how do i use the c# regular to get:

time:Apr 29 2011 13:12:45 UTC
longitude:121.98
latitude 21.18
location:TAIWAN REGION
depth: 177

Thank you very much.
Posted
Comments
Sergey Alexandrovich Kryukov 1-May-11 21:12pm    
Make yourself clear. Regular Expressions? Regex? Tag properly.
--SA
Sergey Alexandrovich Kryukov 1-May-11 21:13pm    
I don't believe 2011 could be a key word.
If you need to parse data based on keywords, explain that.
--SA
Member 1855608 1-May-11 21:51pm    
sorry , it's not key word,is the word that i want.
that i need is the regular expressions.

1 solution

Try this regexp
^at\s+(?<time>.+?),.+?\(.+?longitude\s+(?<longitude>.+?),latitude\s+(?<latitude>.+?)\)\s+in\s+(?<location>.+?),the depth.+?\s+(?<depth>\d+)


C#
///  A description of the regular expression:
///  
///  ^at\s+
///      Beginning of line or string
///      at
///      Whitespace, one or more repetitions
///  [time]: A named capture group. [.+?]
///      Any character, one or more repetitions, as few as possible
///  ,.+?\(.+?longitude\s+
///      ,
///      Any character, one or more repetitions, as few as possible
///      Literal (
///      Any character, one or more repetitions, as few as possible
///      longitude
///      Whitespace, one or more repetitions
///  [longitude]: A named capture group. [.+?]
///      Any character, one or more repetitions, as few as possible
///  ,latitude\s+
///      ,latitude
///      Whitespace, one or more repetitions
///  [latitude]: A named capture group. [.+?]
///      Any character, one or more repetitions, as few as possible
///  \)\s+in\s+
///      Literal )
///      Whitespace, one or more repetitions
///      in
///      Whitespace, one or more repetitions
///  [location]: A named capture group. [.+?]
///      Any character, one or more repetitions, as few as possible
///  ,the depth.+?\s+
///      ,the
///      Space
///      depth
///      Any character, one or more repetitions, as few as possible
///      Whitespace, one or more repetitions
///  [depth]: A named capture group. [\d+]
///      Any digit, one or more repetitions
///  
Regex r= new Regex(
      "^at\\s+(?<time>.+?),.+?\\(.+?longitude\\s+(?<longitude>.+?),"+
      "latitude\\s+(?<latitude>.+?)\\)\\s+in\\s+(?<location>.+?),th"+
      "e depth.+?\\s+(?<depth>\\d+)",
    RegexOptions.IgnoreCase
    | RegexOptions.Compiled
    );

string message = "At Apr 29 2011 13:12:45 UTC,happend an earthquake(near longitude 121.98,latitude 21.18) in TAIWAN REGION,the depth is about 177km";

Match m = r.Match(message);

if (m.Success)
{
  Console.WriteLine("time:" + m.Groups["time"].Value);
  Console.WriteLine("longitude:" + m.Groups["longitude"].Value);
  Console.WriteLine("latitude:" + m.Groups["latitude"].Value);
  Console.WriteLine("location:" + m.Groups["location"].Value);
  Console.WriteLine("depth:" + m.Groups["depth"].Value);
}


This will give you an output like this:

MSIL
time:Apr 29 2011 13:12:45 UTC
longitude:121.98
latitude 21.18
location:TAIWAN REGION
depth: 177
 
Share this answer
 
v2
Comments
nit_singh 3-May-11 3:56am    
my 5..u r genius
Kim Togo 3-May-11 3:58am    
Thanks :-)

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