简体   繁体   中英

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.

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| in front, and |0 at the end.
  • and also I need to append the timestamp value (the last value in the original one) |1395061255413

How can I do this with Notepad++?

Thanks for any help !

Here is how you could do that in Notepad++ :

  1. use Ctrl + H to open the Replace pop-up
  2. tick Regular expression in the Search mode section
  3. search for this pattern: .*?"mLongitude":(\\d+(?:\\.\\d+)?).*?"mLatitude":(\\d+(?:\\.\\d+)).*
  4. replace the matched string by \\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,
  • a literal " | " and
  • the mLatitude value.

Would you like to have this pattern explained in more details, please check out this permalink on 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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