深度剖析华为Python笔试题,竟解锁12种正则表达式技巧?
- 内容介绍
- 文章标签
- 相关推荐
本文共计912个文字,预计阅读时间需要4分钟。
今天,这里是程序员工会晚宴。今天我们用一道华为笔试题深入掌握一项Python技巧:正则表达式。
题目解析:本题目要求我们使用正则表达式实现特定的字符串匹配或提取。
常用方法:
1.使用 `re` 模块中的 `search()` 方法查找第一个匹配的子串。
2.使用 `re` 模块中的 `findall()` 方法查找所有匹配的子串。
3.使用 `re` 模块中的 `sub()` 方法替换字符串中的匹配部分。
专业方法:
1.使用正则表达式中的元字符,如 `.`、`*`、`+`、`?`、`[]`、`^`、`$` 等。
2.使用分组 `()` 和引用 `\1` 等。
注意事项:
1.正则表达式中的点号 `.` 通常匹配除换行符之外的任何字符,需要使用 `re.DOTALL` 标志使其也匹配换行符。
2.在使用 `re.search()` 或 `re.findall()` 时,返回的匹配对象可以进一步使用方法如 `.group()` 获取匹配的文本。
3.在替换操作中,可以使用捕获组(在分组中使用 `\1` 到 `\9`)来引用匹配的子串。
示例代码:
pythonimport re示例字符串text=Hello, world! This is a test. 123456
匹配所有数字numbers=re.findall(r'\d+', text)print(numbers) # 输出: ['123456']
替换所有数字为星号replaced_text=re.sub(r'\d+', '*', text)print(replaced_text) # 输出: 'Hello, world! This is a test. *'
查找所有包含特定子串的行with open('example.txt', 'r') as file: for line in file: if re.search(r'python', line): print(line.strip())
通过以上解析和示例,我们可以更深入地理解和应用正则表达式在Python编程中的技巧。
大家好,这里是程序员晚枫。
今天用一道华为笔试题,带大家深入掌握一项Python技巧:正则表达式。
本文主要分为4个部分:题目解析、常用方法、专业方法、注意事项
一、题目解析
先来看一下题目。
看完题目,有以下2个解题思路:
1、思路1:纯手写
def cut_8ch(str): if len(str) < 8: str = str.ljust(8, '0') elif len(str) > 8: if (len(str) % 8) != 0: width = len(str) + (8 - len(str) % 8) str = str.ljust(width, '0') str2List = [] i = 0 while i < len(str): if (i + 8) < len(str): str2List.append(str[i:i+8]) else: str2List.append(str[i:len(str)]) break i = i + 8 return str2Listoutput = []tmp = input('请输入字符串-->>').strip()output.append(cut_8ch(tmp))
for x in output:for y in x:print(y)
### 2、思路2:使用内置方法和标准库 ```python import re str = input('请输入字符串-->>') if len(s) % 8 != 0: s = s.ljust(len(s) + (8 - len(s) % 8), str(0)) res = re.findall('.{8}', s) [print(r) for r in res]很明显,思路2实现起来,逻辑更清晰,代码更简洁。原因在哪里呢?
今天我们重点讲一下re模块的使用。关于str的所有自带方法,如果大家想看的话,可以在评论区告诉我,我可以另写一个篇新的文章来介绍。
二、常用方法
按照惯例,我们对Python知识的解析,直接拿源码来研究。先看一下python源码里,re模块提供的12个方法
本文共计912个文字,预计阅读时间需要4分钟。
今天,这里是程序员工会晚宴。今天我们用一道华为笔试题深入掌握一项Python技巧:正则表达式。
题目解析:本题目要求我们使用正则表达式实现特定的字符串匹配或提取。
常用方法:
1.使用 `re` 模块中的 `search()` 方法查找第一个匹配的子串。
2.使用 `re` 模块中的 `findall()` 方法查找所有匹配的子串。
3.使用 `re` 模块中的 `sub()` 方法替换字符串中的匹配部分。
专业方法:
1.使用正则表达式中的元字符,如 `.`、`*`、`+`、`?`、`[]`、`^`、`$` 等。
2.使用分组 `()` 和引用 `\1` 等。
注意事项:
1.正则表达式中的点号 `.` 通常匹配除换行符之外的任何字符,需要使用 `re.DOTALL` 标志使其也匹配换行符。
2.在使用 `re.search()` 或 `re.findall()` 时,返回的匹配对象可以进一步使用方法如 `.group()` 获取匹配的文本。
3.在替换操作中,可以使用捕获组(在分组中使用 `\1` 到 `\9`)来引用匹配的子串。
示例代码:
pythonimport re示例字符串text=Hello, world! This is a test. 123456
匹配所有数字numbers=re.findall(r'\d+', text)print(numbers) # 输出: ['123456']
替换所有数字为星号replaced_text=re.sub(r'\d+', '*', text)print(replaced_text) # 输出: 'Hello, world! This is a test. *'
查找所有包含特定子串的行with open('example.txt', 'r') as file: for line in file: if re.search(r'python', line): print(line.strip())
通过以上解析和示例,我们可以更深入地理解和应用正则表达式在Python编程中的技巧。
大家好,这里是程序员晚枫。
今天用一道华为笔试题,带大家深入掌握一项Python技巧:正则表达式。
本文主要分为4个部分:题目解析、常用方法、专业方法、注意事项
一、题目解析
先来看一下题目。
看完题目,有以下2个解题思路:
1、思路1:纯手写
def cut_8ch(str): if len(str) < 8: str = str.ljust(8, '0') elif len(str) > 8: if (len(str) % 8) != 0: width = len(str) + (8 - len(str) % 8) str = str.ljust(width, '0') str2List = [] i = 0 while i < len(str): if (i + 8) < len(str): str2List.append(str[i:i+8]) else: str2List.append(str[i:len(str)]) break i = i + 8 return str2Listoutput = []tmp = input('请输入字符串-->>').strip()output.append(cut_8ch(tmp))
for x in output:for y in x:print(y)
### 2、思路2:使用内置方法和标准库 ```python import re str = input('请输入字符串-->>') if len(s) % 8 != 0: s = s.ljust(len(s) + (8 - len(s) % 8), str(0)) res = re.findall('.{8}', s) [print(r) for r in res]很明显,思路2实现起来,逻辑更清晰,代码更简洁。原因在哪里呢?
今天我们重点讲一下re模块的使用。关于str的所有自带方法,如果大家想看的话,可以在评论区告诉我,我可以另写一个篇新的文章来介绍。
二、常用方法
按照惯例,我们对Python知识的解析,直接拿源码来研究。先看一下python源码里,re模块提供的12个方法

