-1

I am trying to write a regex expression in python that can match the following lines - I am just able to match the very first number by doing something like this enter image description here re.compile(r'\d.\d{14}\s+')

but could not do rest. Also tried doing [^-\d] to catch the negative sign - does not seem working.

Any help? Thanks!

4
  • What exactly are you trying to match? The entire line or the individual numbers?
    – Jesse
    Commented Dec 2, 2021 at 18:29
  • @Jesse - entire line - thanks!
    – saya
    Commented Dec 2, 2021 at 18:30
  • 1
    individual numbers: /-?\d\.\d{14}/ could works. Entire line works with: (-?\d\.\d{14})\s(-?\d\.\d{14})\s(-?\d\.\d{14})\s([oh]) (regexp101 link)[regex101.com/r/o4fdnA/1] Commented Dec 2, 2021 at 18:35
  • Please post your data as text, not image.
    – Toto
    Commented Dec 2, 2021 at 19:45

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.