Python Forum
Regular Expressions - so close yet so far
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expressions - so close yet so far
#6
Oh, right. Lookbehind patterns must be fixed-width. Can't put an optional piece in them. In that case I'd just use a regular search and capture the number.

import re

text = """WEIGHT: 18. 520, 0 0 KGS
WEIGHT: 18. 583, 000 KGS
WEIGHT 6000. 0000 KG
WEIGHT: 17. 624, 00 KGS
WEIGHT: 17. 046, 00 KGS
WEIGHT; 16. 235, 86 KGS
WEIGHT; 13. 672
WEIGHT: 29. 631, 000
WEIGHT: 218768. 000 KGS
WEIGHT: 15 MT
WEIGHT; 14. 834, 32 KGS
WEIGHT; 11. 311, 08 KG"""

for entry in text.splitlines():
    entry = entry.replace(" ", "").lower()
    print(entry, end=" => ")

    if m := re.search('weight[;:]?([\d,.]+)kg', entry):
        print(m.group(1))
    else:
        print("NO MATCH")
Output:
weight:18.520,00kgs => 18.520,00 weight:18.583,000kgs => 18.583,000 weight6000.0000kg => 6000.0000 weight:17.624,00kgs => 17.624,00 weight:17.046,00kgs => 17.046,00 weight;16.235,86kgs => 16.235,86 weight;13.672 => NO MATCH weight:29.631,000 => NO MATCH weight:218768.000kgs => 218768.000 weight:15mt => NO MATCH weight;14.834,32kgs => 14.834,32 weight;11.311,08kg => 11.311,08
The inconsistent separators is more annoying. If you had something like 18,000, how would you be sure which way to interpret?

Assuming you want a period as a decimal separator, you could look if a comma occurs later than a period in the string. If it does, translate them to each other. If you only have one of the two, I don't have a good suggestion.
Reply


Messages In This Thread
RE: Regular Expressions - so close yet so far - by bowlofred - May-03-2023, 08:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Use or raw string on regular expressions Zaya_pool 5 277 May-09-2024, 06:10 PM
Last Post: Zaya_pool
Information Do regular expressions still need raw strings? bobmon 3 330 May-03-2024, 09:05 AM
Last Post: rishika24
  Recursive regular expressions in Python risu252 2 1,325 Jul-25-2023, 12:59 PM
Last Post: risu252
  Having trouble with regular expressions mikla 3 2,651 Mar-16-2021, 03:44 PM
Last Post: bowlofred
  Regular Expressions pprod 4 3,140 Nov-13-2020, 07:45 AM
Last Post: pprod
  Format phonenumbers - regular expressions Viking 2 1,945 May-11-2020, 07:27 PM
Last Post: Viking
  regular expressions in openpyxl. format picnic 0 2,516 Mar-28-2020, 09:47 PM
Last Post: picnic
  Unexpected (?) result with regular expressions guraknugen 2 2,272 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Strange output with regular expressions newbieAuggie2019 1 1,957 Nov-04-2019, 07:06 PM
Last Post: newbieAuggie2019
  Regular Expressions amitalable 4 2,806 Mar-14-2019, 04:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020