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

  • SEARCH
  • Home
  • 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 6816889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:58:49+00:00 2026-05-26T20:58:49+00:00

Original I want to parse a string of html code and add newlines after

  • 0

Original

I want to parse a string of html code and add newlines after closing tags + after the initial form tag. Here’s the code so far. It’s giving me an error in the “re.sub” line. I don’t understand why the regex fails.

def user(): 
    tags = "<form><label for=\"email_field\">Email:</label><input type=\"email\" name=\"email_field\"/><label for=\"password_field\">Password:</label><input type=\"password\" name=\"password_field\"/><input type=\"submit\" value=\"Login\"/></form>"
    result = re.sub("(</.*?>)", "\1\n", tags)
    return dict(form_code=result)

PS. I have a feeling this might not be the best way… but I still want to learn how to do this.

EDIT

I was missing “import re” from my default.py. Thanks ruakh for this.

import re

Now my page source code shows up like this (inspected in client browser). The actual page shows the form code as text, not as UI elements.

&lt;form&gt;&lt;label for=&quot;email_field&quot;&gt;Email:&lt;/label&gt;
&lt;input type=&quot;email&quot; name=&quot;email_field&quot;/&gt;&lt;label     
for=&quot;password_field&quot;&gt;Password:&lt;/label&gt;
&lt;input type=&quot;password&quot; name=&quot;password_field&quot;/&gt;&lt;input   
type=&quot;submit&quot; value=&quot;Login&quot;/&gt;&lt;/form&gt;

EDIT 2

The form code is rendered as UI elements after adding XML() helper into default.py. Thanks Anthony for helping. Corrected line below:

return dict(form_code=XML(result))

FINAL EDIT

Fixing the regex I figured myself. This is not optimal solution but at least it works. The final code:

import re
def user(): 
    tags = "<form><label for=\"email_field\">Email:</label><input type=\"email\" name=\"email_field\"/><label for=\"password_field\">Password:</label><input type=\"password\" name=\"password_field\"/><input type=\"submit\" value=\"Login\"/></form>"
    tags = re.sub(r"(<form>)", r"<form>\n  ", tags)
    tags = re.sub(r"(</.*?>)", r"\1\n  ", tags)
    tags = re.sub(r"(/>)", r"/>\n  ", tags)
    tags = re.sub(r"(  </form>)", r"</form>\n", tags)
    return dict(form_code=XML(tags))
  • 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-05-26T20:58:50+00:00Added an answer on May 26, 2026 at 8:58 pm

    The only issue I see is that you need to change "\1\n" to r"\1\n" (using the “raw” string notation); otherwise \1 is interpreted as an octal escape (meaning the character U+0001). But that shouldn’t give you an error, per se. What error-message are you getting?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

original string is a,d,k I want to remove all , and make it to
I have a photo website and i want to support tags as my original
I'm using pyparsing to parse HTML. I'm grabbing all embed tags, but in some
I have a function that returns a string of HTML code for a jQuery
Original Question I want to be able to generate a new (fully valid) MP3
I want to use YUI Compressor (the original) and use it as part of
i want the draggable object to revert back to its original position when i
Original Problem In building our projects, I want the mercurial id of each repository
I want to use a category to make a method on the original class
UPDATE: I should have mentioned in the original post that I want to learn

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.