Python Forum
[Regex] Findall returns wrong number of hits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Regex] Findall returns wrong number of hits
#1
Hello,

It's probably something obvious to seasoned Python programmers, but I can't figure out why re.findall() returns the wrong number of hits when using the following code :
import re,sys,locale

# Open file
f = open('input.gpx', 'r')

#OK
#strings = re.findall(r'<trk>', f.read())
#BAD!
strings = re.findall(r'<trk>.+?</trk>', f.read())

f.close()

if strings:
	#27 instead of 348!
	print "Number of items : ", len(strings)
The file contains some French characters. Could it be that accented characters in the input file are preventing Python from reading the whole file?

FWIW, I'm using Python 2.7.14 on Windows.

Thank you.
Reply


Messages In This Thread
[Regex] Findall returns wrong number of hits - by Winfried - Aug-21-2018, 03:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  regex findall() returning weird result Radical 1 588 Oct-15-2023, 08:47 PM
Last Post: snippsat
  np.percentile returns wrong value? AceTylercholine 2 619 Jul-13-2023, 06:59 PM
Last Post: Skaperen
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,386 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,173 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  '|' character within Regex returns a tuple? pprod 10 5,463 Feb-19-2021, 05:29 PM
Last Post: eddywinch82
  regex.findall that won't match anything xiaobai97 1 1,971 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,102 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  Programme will not returns the day number not the day name Oldman45 8 2,979 Jul-27-2020, 11:29 AM
Last Post: Oldman45
  Regex findall() NewBeie 2 4,235 Jul-10-2020, 12:19 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