0

I am trying to do a regex search on some text where I am only interested in the text between some patterns.

Sample text:

<h2><font color='#fff'>some text </font></h2><HR noshade size="5" width="50%" align="center"><table><tr><th id='t1'>Host  </th><th>10.0.1.1</th></tr><th id='t1'>Port  </th><th>8080</th></tr><th id='t1'>User  </th><th>chris</th></tr><th id='t1'>Password  </th><th>chris</th></tr></table><h4><font color='#fff'>
...
<h2><font color='#fff'>some more text </font></h2><HR noshade size="5" width="50%" align="center"><table><tr><th id='t1'>Host  </th><th>10.0.1.2</th></tr><th id='t1'>Port  </th><th>9090</th></tr><th id='t1'>User  </th><th>bob</th></tr><th id='t1'>Password  </th><th>bob</th></tr></table><h4><font color='#fff'>

This is my regex:

Host.*?<th>(.*?)<.*Port.*?<th>(.*?)<.*User.*?<th>(.*?)<.*Password.*?<th>(.*?)<

Each regex match is returning a list and that is not what I want. I would like the groups to be combined into a string.

This is the output I want:

10.0.1.1 8080 chris chris
10.0.1.2 9090 bob bob

Here is what I am doing:

lines = []
lines.extend(re.findall(r"Host.*?<th>(.*?)<.*Port.*?<th>(.*?)<.*User.*?<th>(.*?)<.*Password.*?<th>(.*?)<", s))
print (lines)

Which gives me:

[('10.0.1.1', '8080', 'chris', 'chris'), ('10.0.1.2', '9090', 'bob', 'bob')]

Can anyone exaplin why this happens and how I can get what I want?

Thanks, Chris

3
  • You get a list of tuples back because findall returns an list of matches, which in this case are tuples. Have you tried using join to join elements of each tuple? Commented Jun 17, 2021 at 20:19
  • 1
    print('\n'.join([' '.join(values) for values in lines])) Commented Jun 17, 2021 at 20:24
  • Thank you Justin Commented Jun 17, 2021 at 20:45

1 Answer 1

1

re.findall returns a list of tuples for each matching group when you include two or more parentheses. You can further process that to match what you want:

strings = [' '.join(tup) for tup in output]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.