Python Forum
[SOLVED] Why does regex fail cleaning line?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Why does regex fail cleaning line?
#4
This works as long as there is no > in the part that you want to remove
import re

src = """\
<?xml version="1.0" encoding="UTF-8"?>
<gpx 
 xmlns="http://www.topografix.com/GPX/1/1" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" 
 version="1.1">
 <trk>
  <trkseg>
   <trkpt lat="45.649872" lon="0.156119"><ele>101.25</ele></trkpt>
      <trkpt lat="43.929379" lon="2.147619"><ele>178</ele></trkpt>
   <trkpt lat="43.929388" lon="2.147699"><ele>177.75</ele></trkpt>
  </trkseg>
 </trk>
</gpx>
"""

res = re.sub(r'<gpx\b[^>]*>', '<gpx>', src)
print(res)
Output:
<?xml version="1.0" encoding="UTF-8"?> <gpx> <trk> <trkseg> <trkpt lat="45.649872" lon="0.156119"><ele>101.25</ele></trkpt> <trkpt lat="43.929379" lon="2.147619"><ele>178</ele></trkpt> <trkpt lat="43.929388" lon="2.147699"><ele>177.75</ele></trkpt> </trkseg> </trk> </gpx>
Reply


Messages In This Thread
RE: Why does regex fail cleaning line? - by Gribouillis - Aug-22-2021, 05:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 842 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 670 Oct-18-2023, 11:09 PM
Last Post: Edward_
  Cleaning my code to make it more efficient BSDevo 13 1,465 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Help with a regex? (solved) wrybread 3 869 May-01-2023, 05:12 AM
Last Post: deanhystad
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,126 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 1,892 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  How to calculated how many fail in each site(s) in csv files SamLiu 4 1,329 Sep-26-2022, 06:28 AM
Last Post: SamLiu
  regex multi-line kucingkembar 6 1,648 Aug-27-2022, 10:27 PM
Last Post: kucingkembar
  Apply textual data cleaning to several CSV files ErcoleL99 0 867 Jul-09-2022, 03:01 PM
Last Post: ErcoleL99
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,643 Mar-11-2022, 01:50 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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