0

I have the following code that checks if there is a formular content in an email body but I did not understand what does this string '<\s?\/?\s?form\s?>' means and if there is another method to check formular content existence in an email?

This is the code I wrote:

class HTMLFormFinder(FeatureFinder):
    def getFeature(self, message):
        import re
        super(HTMLFormFinder, self).getFeature(message)
        payload = utils.getpayload(message).lower()
        return re.compile(r'<\s?\/?\s?form\s?>', re.IGNORECASE).search(payload)!= None

Thanks in advance.

2
  • if you have HTML then use BeautifulSoup or lxml Commented Jul 15, 2019 at 15:45
  • 1
    You can use use regex101 to find out what each regular expression means (upper right side by default) if you don't want to read up on regexes as a whole. Commented Jul 15, 2019 at 15:48

2 Answers 2

1

It's what's called a regular expression. It's a way to match strings that follow a particular pattern.

https://docs.python.org/3.7/library/re.html

Here r'<\s?\/?\s?form\s?>' describes a <form> HTML tag with several fallbacks in case of bad/malformed html, specifically it handles whitespaces that may appear beside the tag name form.

A better way of checking the presence of forms is to use an XML/HTML parser, like ElementTree, BeautifulSoup, because they handle bad/incorrect HTML much better than regular expressions ever can. But if you want to keep it simple, the regex you have should suffice.

Using BeautifulSoup you can do:

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
has_form = len(soup.find('form')) > 0
Sign up to request clarification or add additional context in comments.

Comments

0

You can read more on regular expressions here: https://docs.python.org/2/library/re.html

Specifically \s matches any whitespace charachter.

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.