Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:38:15+00:00 2026-06-10T15:38:15+00:00

I have some well-behaved xml files I want to reformat (NOT PARSE!) using regex.

  • 0

I have some well-behaved xml files I want to reformat (NOT PARSE!) using regex. The goal is to have every <trkpt> pairs as oneliners.

The following code works, but I’d like to get the operations performed in a single regex substitution instead of the loop, so that I don’t need to concatenate the strings back.

import re

xml = """
    <trkseg>
      <trkpt lon="-51.2220657617" lat="-30.1072524581">
        <time>2012-08-25T10:20:44Z</time>
        <ele>0</ele>
      </trkpt>
      <trkpt lon="-51.2220657617" lat="-30.1072524581">
        <time>2012-08-25T10:20:44Z</time>
        <ele>0</ele>
      </trkpt>
      <trkpt lon="-51.2220657617" lat="-30.1072524581">
        <time>2012-08-25T10:20:44Z</time>
        <ele>0</ele>
      </trkpt>
    </trkseg>
"""

for trkpt in re.findall('<trkpt.*?</trkpt>', xml, re.DOTALL):
    print re.sub('>\s*<', '><', trkpt, re.DOTALL)

An answer using sed would also be welcome.

Thanks for reading

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:38:16+00:00Added an answer on June 10, 2026 at 3:38 pm

    How about this:

    >>> regex = re.compile(
        r"""\n[ \t]*  # Match a newline plus following whitespace
        (?=           # only if... 
         (?:          # ...the following can be matched:
          (?!<trkpt)  #  (unless an opening <trkpt> tag occurs first)
          .           #  any character
         )*           # any number of times,
         </trkpt>     # followed by a closing </trkpt> tag
        )             # End of lookahead""", 
        re.DOTALL | re.VERBOSE)
    >>> print regex.sub("", xml)
    
        <trkseg>
          <trkpt lon="-51.2220657617" lat="-30.1072524581"><time>2012-08-25T10:20:44Z</time><ele>0</ele></trkpt>
          <trkpt lon="-51.2220657617" lat="-30.1072524581"><time>2012-08-25T10:20:44Z</time><ele>0</ele></trkpt>
          <trkpt lon="-51.2220657617" lat="-30.1072524581"><time>2012-08-25T10:20:44Z</time><ele>0</ele></trkpt>
        </trkseg>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some non well-formed xml (HTML) data in JAVA, I used JAXP Dom,
well i have a listbox with some items inside. i want to detect a
I have some login code that works well for Firefox and other browsers, but
I know java well, and have some experience in EclipseLink, Hibernate, JSF, Grails and
I have done some project in Irrlicht and it compiles all well, however when
I have requirement where some times I would like to load children as well
I have a State class that includes configuration as well as some state-specific methods.
I have an application written in C# that invokes some C code as well.
I have an activity that shows some TextView s and EditText s, as well
Well, I have a application which somehow requires some system resources, but how do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.