使用正则表达式从专有文本格式获取地理十进制坐标

Obtaining geographic decimal coordinates from proprietary text format using regex

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

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

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

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

想法很简单:

  • 第一步将所有长度小于6的数补全为6 之前的零以确保长度应大于 6
  • 在第二步中,在第 6 个数字前加上点
  • 第三步将点之前的所有多个零替换为一个

最后输出

-123.456789 
123.456789
-23.456789
0.056789
-0.000089

检查三个步骤:

您需要在 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+)

替换为:

(?2.:0.)000000           # 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.

替换为:

.

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

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