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 145517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:26:16+00:00 2026-05-11T08:26:16+00:00

I am just trying to write a small web page that can parse some

  • 0

I am just trying to write a small web page that can parse some text using a regular expression and return the resulting matches in a table. This is the first I’ve used python for web development, and I have to say, it looks messy.

My question is why do I only get output for the last match in my data set? I figure it has to be because the nested loops aren’t formatted correctly.

Here’s the data I provide:

groups is just an id correspoding to the regex group, and it’s name to provide the header for the table.

pattern is something like:

(\d+)\s(\S+)\s(\S+)$ 

and data:

12345 SOME USER 09876 SOMEONE ELSE 54678 ANOTHER USER 

My simple page:

<% import re pattern = form['pattern'] p = re.compile(pattern) data = form['data']  matches = p.finditer(data)  lines = form['groups'].split('\n') groupids ={} for line in lines:   key, val = line.split(' ')   groupids[int(key.strip())] = val.strip()  %> <html> <table style='border-width:1px;border-style:solid;width:60%;'> <tr> <% for k,v in groupids.iteritems():%> <th style='width:30px;text-align:center'><%= v %></th> <% # end %> </tr> <% for match in matches:   #begin %><tr> <% for i in range(1, len(match.groups())+1):   #begin %>   <td style='border-style:solid;border-width:1px;border-spacing:0px;text-align:center;'><%= match.group(i) %></td> <%   #end # end %> </tr>  </table> </html> 

Edit

Below is the test I ran

Code:

import re pattern = '(\d\d\d\d\d)\s(\S+)\s(\S+)'  p = re.compile(pattern)  data = '''12345 TESTS USERS 34567 TESTS USERS 56789 TESTS USERS'''  groups = '''1 PIN 2 FNAME 3 LNAME'''  matches = p.finditer(data)  lines = groups.split('\n')  print lines groupids ={} for line in lines:   key, val = line.split(' ')   groupids[int(key.strip())] = val.strip()   for k,v in groupids.iteritems():   print '%s\t' % v, print ''  for match in matches:   for i in range(1, len(match.groups())+1):     print '%s\t' % match.group(i),   print '' 

Output:

PIN     FNAME   LNAME 12345   TESTS   USERS 34567   TESTS   USERS 56789   TESTS   USERS 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T08:26:16+00:00Added an answer on May 11, 2026 at 8:26 am
    <% for match in matches:   #begin %><tr> <% for i in range(1, len(match.groups())+1):   #begin %>   <td style='border-style:solid;border-width:1px;border-spacing:0px;text-align:center;'><%= match.group(i) %></td> <%   #end # end %> 

    Yeah, you haven’t got a nested loop there. Instead you’ve got a loop over matches that outputs “<tr>\n”, then a second loop over range(...) that only runs after the first has finished. The second is not inside the first because it isn’t indented to say so.

    From the doc, I think what you need to be saying is:

    <% for match in matches:     # begin %><tr><%     for group in match.groups():         # begin %><td style='border-style:solid;border-width:1px;border-spacing:0px;text-align:center;'><%= group %></td><%     # end %></tr><% # end %> 

    But I can only agree with your “messy” comment: if PSP is requiring that you torture the indenting of your HTML to fit the structure of your Python like this, it is really Doing It Wrong and you should look for another, less awful templating syntax. There are many, many templating languages for Python that have a more sensible syntax for control structures. As an example, in the one I use the above would look like:

    <px:for item='match' in='matches'><tr>     <px:for item='group' in='match.groups()'>         <td style='border-style:solid;border-width:1px;border-spacing:0px;text-align:center;'>             <?_ group ?>         </td>     </px:for> </tr></px:for> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a web application using SpringMVC. Normally I'd just map some
I'm trying to write (or just find an existing) PHP method that can take
Im trying to write a small web application for forwarding requests to my page
Hey i'm just trying to write some code in DrScheme: ((function (x) (* x
I've been trying to write a small library using Thor to help assist me
I'm trying to write a small baseball statistics program using data from retrosheet.org, but
I am trying to write a small piece of Actionscript that gives focus to
I am trying to write a small Cocoa application that helps me manage my
I am trying to write a small music manager in Linux using C++. I
Using the CoreScanner Driver provided by Motorola, I'm trying to write a small winforms

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.