python – regex to match coordinates

First, lets start by looking at the numbers. You’ve already got a decent expression for finding a single number (d.d{14}s+), but there are a couple things wrong with it.

  1. In regex, . indicates any single character. This means that your expression will accept any character after the first digit.
  2. It’s not taking into account the possibility that there could be a negative sign at the beginning.

Both of these problems are really easy to fix. The first can be fixed by simply escaping the period (.). The second can be fixed by adding the negative sign to the pattern and giving it a quantifier. In this case, the ? quantifier will be the best option because it matches between 0 and 1 times. All this means is that it won’t care if the symbol is there, but if it is it will match it. After these 2 changes, the pattern looks like this: -?d.d{14}s+.

Next, we need to tell it to match more than once. This can be done very easily by putting the pattern in a group and applying a quantifier to said group. Now the question is which quantifier should be used. In your example, there are only 3 numbers before the single character at the end of the line. You can match this pattern exactly 3 times by using the {3} quantifier. If you know there will be at least 1 but don’t know how many in total there will be, you can use the + quantifier. For this example I will be using the {3} quantifier just so it’s more specific to your question. After adding this, the pattern will look something like this: (-?d.d{14}s+){3}

Now all that’s left is to match the character at the end. You can use S to match any single word character. You can add a quantifier to it, but again, for the purposes of your question, I won’t be since there’s only a single character. The final expression would look like (-?d.d{14}s+){3}S.

Read more here: Source link