本文介绍了如何使用常规获取关键词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮帮我.
关键字如下:
在UTC 2011年4月29日13:12:45,台湾发生地震(东经121.98附近,北纬21.18附近),深度约为177公里."

我该如何使用C#Regular来获取:

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.

推荐答案

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





///  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:

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


这篇关于如何使用常规获取关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 15:47