简体   繁体   English

如何在Notepad ++中搜索和替换为正则表达式

[英]How to search and replace with regex in Notepad++

I have a txt files with some lines containing GPS data, which I need to shorten. 我有一个txt文件,其中有些行包含GPS数据,我需要将其缩短。

So I have 所以我有

5|{"mResults":0.0|0.0|"mProvider":"fused"|"mDistance":0.0|"mTime":1395061255413|"mAltitude":161.0|"mLongitude":29.0459152|"mLon2":0.0|"mLon1":0.0|"mLatitude":41.0854122|"mLat1":0.0|"mLat2":0.0|"mInitialBearing":0.0|"mHasSpeed":true|"mHasBearing":false|"mHasAltitude":true|"mHasAccuracy":true|"mAccuracy":15.0|"mSpeed":0.425211|"mBearing":0.0}|1395061255413

and I need to extract only the coordinates, so convert it into this : 而且我只需要提取坐标,因此将其转换为:

29.0459152|41.0854122

Edit: 编辑:

Turns out I need this: 原来我需要这个:

GPS|29.0459152|41.0854122|0|1395061255413

Please note that I need to : 请注意,我需要:

  • add GPS| 添加GPS| in front, and |0 at the end. 在前面, |0在最后。
  • and also I need to append the timestamp value (the last value in the original one) |1395061255413 并且我还需要附加时间戳记值(原始值中的最后一个值) |1395061255413

How can I do this with Notepad++? 如何使用Notepad ++做到这一点?

Thanks for any help ! 谢谢你的帮助 !

Here is how you could do that in Notepad++ : 这是您可以在Notepad ++中执行的操作

  1. use Ctrl + H to open the Replace pop-up 使用Ctrl + H打开“ 替换”弹出窗口
  2. tick Regular expression in the Search mode section Search mode部分打勾 Regular expression
  3. search for this pattern: .*?"mLongitude":(\\d+(?:\\.\\d+)?).*?"mLatitude":(\\d+(?:\\.\\d+)).* 搜索此模式: .*?"mLongitude":(\\d+(?:\\.\\d+)?).*?"mLatitude":(\\d+(?:\\.\\d+)).*
  4. replace the matched string by \\1|\\2 匹配的字符串替换\\1|\\2
  5. click on Replace all ;) 单击全部替换 ;)

How it works: 这个怎么运作:

The regex pattern featured in my answer extracts the values for the longitude and latitude from each line and replaces the whole line by: 我的答案中的正则表达式模式从每行中提取经度和纬度的值并将整行替换为

  • the mLongitude value, mLongitude值,
  • a literal " | " and 文字“ | ”和
  • the mLatitude value. mLatitude值。

Would you like to have this pattern explained in more details, please check out this permalink on regex101 . 您想更详细地说明此模式吗,请在regex101上查看此固定链接


EDIT: 编辑:

To include the timestamp you're referring to, you need to use this regex: 要包括您要参考的时间戳 ,您需要使用以下正则表达式:

.*?"mLongitude":(\d+(?:\.\d+)?).*?"mLatitude":(\d+(?:\.\d+)).*\|(\d+)

Then, you just have to change the formatting of your replacement string to this: 然后,您只需要将替换字符串的格式更改为此:

GPS|\1|\2|0|\3

You probably got that already but let's still write down the complete usable solution ;) 您可能已经知道了,但让我们仍然写下完整的可用解决方案;)

I hope this helps! 我希望这有帮助!

I solved my problem by this : 我这样解决了我的问题:

search: 搜索:

.*?"mTime":(\d+(?:\.\d+)?).*?"mLongitude":(\d+(?:\.\d+)).*?"mLatitude":(\d+(?:\.\d+)).*

replace : 替换:

GPS|\2|\3|0|\1

But this is because the same data (timestamp) was also inside the string. 但这是因为相同的数据(时间戳)也位于字符串内。 So this does not extract the timestamp from the end, but from the middle of the string. 因此,这不是从结尾提取时间戳,而是从字符串的中间提取时间戳。

So if @ccjmne edits the answer that extracts it from the end, I will accept that answer. 因此,如果@ccjmne编辑从末尾提取的答案,我将接受该答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM