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

조회 수: 1 (최근 30일)
pietro
pietro 2017년 6월 3일
편집: pietro 2017년 6월 4일
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?
thanks
Regards,
Pietro
The following code works regexp('qu qa','(?!<)\w*(?!>)','match')

채택된 답변

Guillaume
Guillaume 2017년 6월 3일
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')
  댓글 수: 3
Guillaume
Guillaume 2017년 6월 3일
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
pietro 2017년 6월 4일
편집: pietro 2017년 6월 4일
thanks for your reply. I haven't thought of using regexprep

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by