使用正则表达式查找日期的第一个实例

时间:2016-01-05 23:55:30

标签: regex xml date

我试图在SubmlimeText2中查找/替换潮汐数据的xml文件,并且我被同一日期的多个实例难倒。这是一个数据样本:

<item>
  <date>2016/01/01</date>
  <time>06:16 AM</time>
  <predictions_in_ft>8.2</predictions_in_ft>
  <predictions_in_cm>250</predictions_in_cm>
  <highlow>H</highlow>
</item>
<item>
  <date>2016/01/01</date>
  <time>12:31 PM</time>
  <predictions_in_ft>3.0</predictions_in_ft>
  <predictions_in_cm>91</predictions_in_cm>
  <highlow>L</highlow>
</item>
<item>
  <date>2016/01/01</date>
  <time>06:13 PM</time>
  <predictions_in_ft>6.6</predictions_in_ft>
  <predictions_in_cm>201</predictions_in_cm>
  <highlow>H</highlow>
</item>

我尝试做的是保留日期的第一个实例,但删除后续日期以达到此目的:

<item>
  <date>2016/01/01</date>
  <time>06:16 AM</time>
  <predictions_in_ft>8.2</predictions_in_ft>
  <predictions_in_cm>250</predictions_in_cm>
  <highlow>H</highlow>
</item>
<item>
  <time>12:31 PM</time>
  <predictions_in_ft>3.0</predictions_in_ft>
  <predictions_in_cm>91</predictions_in_cm>
  <highlow>L</highlow>
</item>
<item>
  <time>06:13 PM</time>
  <predictions_in_ft>6.6</predictions_in_ft>
  <predictions_in_cm>201</predictions_in_cm>
  <highlow>H</highlow>
</item>

有什么想法吗?我知道某种xml解析器可能会更方便,但我是一个菜鸟,这是按时完成工作。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此模式:

(?|\G(?<=(.{10})</date>\n[ ]{2})[^<]*|<date>([^<]+))(?:<(?!date>)[^<]*)*+\K<date>\1</date>\n[ ]{2}

demo

如果不起作用,请在每个\r之前添加\n

相关问题