使用正则表达式从专有文本格式获取地理小数点坐标

时间:2017-08-27 11:31:56

标签: regex notepad++ pcre

仅使用带有正则表达式支持的Notepad ++我想从txt文件中提取一些数据,表示地理坐标并组织输出:

  • -123456789变为-123.456789
  • 123456789变为123.456789
  • -23456789变为-23.456789
  • 56789变为0.056789
  • -89变为-0.000089

试过这个:(-?)([0-9]*)([0-9]{6})但是当输入长度小于6位时失败

3 个答案:

答案 0 :(得分:0)

您可以通过三个步骤完成此操作:

  • 第1步:将(-?)\b(\d{1,6})\b替换为\10000000\2
  • 第2步:将(-?)(\d{0,})(\d{6})替换为\1\2.\3
  • 第3步:将0{2,}\.替换为0.

这个想法很简单:

  • 在第一步中,将所有小于6的长度的数字与6进行互补 之前确保零长度应该大于6
  • 在第二步中将点放在第六个数字之前
  • 第三步只用一个
  • 替换点前的所有多个零

最后输出

-123.456789 
123.456789
-23.456789
0.056789
-0.000089

检查以下三个步骤:

Step one

Step two

Step three

答案 1 :(得分:0)

你需要在notepad ++中使用2个步骤才能执行此操作。首先,我们来看看正则表达式:

(?<sign>-?)(?<first>\d+(?=\d{6}))?(?<last>\d+)

分组捕获必要的部分。

说明:(如果需要,可以丢失命名分组)

(?<sign>-?)                    # read the '-' sign
(?<first>\d+(?=\d{6}))?        # read as many digits as possible,
                               # leaving 6 digits at the end.
(?<last>\d+)                   # read the remaining digits.

请参阅regex101.com

如何在记事本++中使用它?使用两步搜索并替换:

(-?)(\d+(?=\d{6}))?(\d+)

替换为:

\1(?2\2.:0.)000000\3           # copy sign, if group 2 contains any
                               # values, copy them, followed by '.'.
                               # If not show a '0.'
                               # Print 6 zero's, followed by group 3.

接下来,替换多余的零。

\.(0+(?=\d{6}\b))(\d{6})       # Replace the maximum number of zero's
                               # leaving 6 digits at the end.

替换为:

.\2

答案 2 :(得分:0)

您可以使用可用于记事本++的Python Script插件:

editor.rereplace('(\d+)', lambda m: ('%f' % (float(m.group(1))/1000000)))