MATLAB Answers

Web scraping with regular expression, getting rid of html tags.

11 views (last 30 days)
pietro on 3 Jun 2017
Edited: pietro on 4 Jun 2017
Hi all,
I am doing some webscraping code and consequently, I am using regular expressions. I need to isolate the words from a string, of course html tags should not be included. Html tags are words included in < > (e.g. br). Unfortunately, my code does not work out and I am wondering why. Here an example:
regexp('qu <qa>','(?!<)\w*(?!>)','match')
My expected results is 'qu' but instead I get 'qu' and 'q'. The code works with this string 'qu q'. What may I do to solve this issue?
The following code works regexp('qu qa','(?!<)\w*(?!>)','match')


Sign in to comment.

Accepted Answer

Guillaume on 3 Jun 2017
The first part of your expression is a look-ahead. You want a look behind instead. Add a < before the !:
regexp('qu <qa>', '(?<!<)\w*(?!>)', 'match')


pietro on 3 Jun 2017
Yep!!! Thanks. What a mistake. Nevertheless, I found tags composed by more than a word, like:
<td class="tdat">
How can I cut them out?
Guillaume on 3 Jun 2017
It's a lot more difficult to tell a regular expression not to match something than it is to tell it to match something. Therefore, I'd do it in two passes.
1. remove the tags:
notags = regexprep(yourstring, '<[^>]*>', '')
2. match whatever it is you want to match
matches = regexp(notags, '\w+', 'match')
pietro on 4 Jun 2017
thanks for your reply. I haven't thought of using regexprep

Sign in to comment.

More Answers (0)

Sign in to answer this question.