python Regular Expression interview question Answers

  • 2020-07-21 09:06:32
  • OfStack

Three python regular expression interview questions, as follows

1. Remove labels from the html file below to display only text information.


<div>
<p> Job Responsibilities: </p>
<p> Complete the related work on the server side such as recommendation algorithm, data statistics, interface and background </p>
<p><br></p>
<p> Essential Requirements: </p>
<p> Good self - driven and professional, proactive and result - oriented </p>
<p>&nbsp;<br></p>
<p> Technical requirements: </p>
<p>1 , 1 In the above  Python  Experience in development, object-oriented analysis and design, understanding of design patterns </p>
<p>2 , master HTTP Protocol, familiarity MVC , MVVM And related concepts WEB Development framework </p>
<p>3 · Master the development and design of relational database  SQL , skilled in use  MySQL/PostgreSQL  In the 1 Kind of <br></p>
<p>4 , master NoSQL , MQ , proficient in using corresponding technical solutions </p>
<p>5 , familiar with the  Javascript/CSS/HTML5 . JQuery , React , Vue.js</p>
<p>&nbsp;<br></p>
<p> Pluses: </p>
<p> Big data, Mathematical statistics, machine learning, sklearn , high performance, high concurrency. </p>
</div> 

The sub method in re module of python regular expression is used to replace the label with an empty string. The code is as follows:


#-*- coding:utf-8 -*-
import re

# Remove the label 

s = "<div>\
<p> Job Responsibilities: </p>\
<p> Complete the related work on the server side such as recommendation algorithm, data statistics, interface and background </p>\
<p><br></p>\
<p> Essential Requirements: </p>\
<p> Good self - driven and professional, proactive and result - oriented </p>\
<p>&nbsp;<br></p>\
<p> Technical requirements: </p>\
<p>1 , 1 In the above  Python  Experience in development, object-oriented analysis and design, understanding of design patterns </p>\
<p>2 , master HTTP Protocol, familiarity MVC , MVVM And related concepts WEB Development framework </p>\
<p>3 · Master the development and design of relational database  SQL , skilled in use  MySQL/PostgreSQL  In the 1 Kind of <br></p>\
<p>4 , master NoSQL , MQ , proficient in using corresponding technical solutions </p>\
<p>5 , familiar with the  Javascript/CSS/HTML5 . JQuery , React , Vue.js</p>\
<p>&nbsp;<br></p>\
<p> Pluses: </p>\
<p> Big data, Mathematical statistics, machine learning, sklearn , high performance, high concurrency. </p>\
</div> "

p = r"</?\w+>|&nbsp;"
print(re.sub(p, " ", s))

2. Extract the domain name from:

http://www.interoem.com/messageinfo.asp?id=35`
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377 & id=6
http://www.fincm.com/newslist.asp?id=415

Replace the entire string with a domain-only string using the sub method. The code is as follows:


#-*- coding:utf-8 -*-
import re

# Extract the domain name 
s2 = """http://www.interoem.com/messageinfo.asp?id=35`
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377&id=6
http://www.fincm.com/newslist.asp?id=415"""

p = r"(http://.+?/).+"

print(re.sub(p, lambda x : x.group(1), s2))

3. Extract the words in the following string:


hello world ha ha

Use the split method to split the space or use the findall method to find all the words, the code is as follows:


#-*- coding:utf-8 -*-
import re

# Extract the word 
s3 = "hello world ha ha"
print(re.split(r" +", s3))
print(re.findall(r"\b\w+\b", s3))

The results of the three questions are as follows:

Question 1:

Job Responsibilities: Complete the work related to the server side, such as recommendation algorithm, data statistics, interface, background, etc. Essential requirements: Good self-motivation and professional quality, proactive work, result-oriented technical requirements: 1, 1 years experience in Python development, master the object-oriented analysis and design, understand design pattern 2, grasp HTTP agreement, familiar with MVC, MVVM concepts and related WEB development framework 3, master the relational database development and design, master SQL, skilled use MySQL/PostgreSQL 1 of the 4, master NoSQL, MQ, skilled use of corresponding technical solution 5, familiar with Javascript/CSS/HTML5, JQuery, React, Vue.js Bonus points: Big Data, Mathematical statistics, Machine learning, sklearn, High performance, large concurrency.

Question 2:

http://www.interoem.com/
http://3995503.com/
http://lib.wzmc.edu.cn/
http://www.zy-ls.com/
http://www.fincm.com/

Question 3:

['hello', 'world', 'ha', 'ha']
['hello', 'world', 'ha', 'ha']


Related articles: