2830680393 发表于 2020-4-10 16:14:42

正则表达式

1.编写正则表达式,查找下面文本中所有处于单词开头位置的字母。

Python is a great object-oriented, interpreted, and interactive programming language.


2. 编写正则表达式,查找下面文本中所有处于单词开头位置的数字。

Pyth8on 7is a 3great 2object-oriented, inter4preted, 1an3d interactive programming 5language.

永恒的蓝色梦想 发表于 2020-4-10 16:23:22

本帖最后由 永恒的蓝色梦想 于 2020-4-10 16:25 编辑

问题一from re import findall

string="Python is a great object-oriented, interpreted, and interactive programming language."

result= for i in findall(r'+',string)]

wp231957 发表于 2020-4-10 16:32:09

永恒的蓝色梦想 发表于 2020-4-10 16:23
问题一
看错,请忽略

永恒的蓝色梦想 发表于 2020-4-10 16:33:15

wp231957 发表于 2020-4-10 16:32
看错,请忽略

难道不是吗?

疾风怪盗 发表于 2020-4-10 16:47:18

为什么一定要用正则表达式?
string="Python is a great object-oriented, interpreted, and interactive programming language."
s = string.replace(',','').split(' ')
for n in s:
    print(n)
这样不是可以直接得到么?
第二个问题就是再加个判断,是数字的就输出,用不到正则啊

zltzlt 发表于 2020-4-10 17:30:53

第二个:

from re import findall
string = "Pyth8on 7is a 3great 2object-oriented, inter4preted, 1an3d interactive programming 5language."
result = for i in findall(r'(?: (\d)+)|(?:(\d)[^a-zA-Z]+[^a-zA-Z])', string)]
print(result)

wangka 发表于 2020-4-10 17:41:17

第1个不就是数数吗

hrp 发表于 2020-4-10 17:42:02

本帖最后由 hrp 于 2020-4-10 17:43 编辑

import re
str1 = 'Python is a great object-oriented, interpreted, and interactive programming language.'
str2 = 'Pyth8on 7is a 3great 2object-oriented, inter4preted, 1an3d interactive programming 5language.'
# 要求1
res1 = re.findall(r'(?<=^){1}|(?<=\s){1}', str1)
# 要求2
res2 = re.findall(r'(?<=^)+(?=+)|(?<=\s)+(?=+)', str2)
# 在控制台打印结果列表
print(res1, '\n', res2)

偷偷用手机写的,不知道有没有写错
页: [1]
查看完整版本: 正则表达式