开发者

Regex + Python - Converting multiple commands into one or fewer commands

开发者 https://www.devze.com 2023-03-05 18:17 出处:网络
Does anyone have an idea on how to convert the following commands into one or fewer commands? variable = re.search(\"[a-zA-Z0-9]+[a-zA-Z0-9\\-\' ]+[a-zA-Z0-9]+\", variable).group(0)

Does anyone have an idea on how to convert the following commands into one or fewer commands?

variable = re.search("[a-zA-Z0-9]+[a-zA-Z0-9\-' ]+[a-zA-Z0-9]+", variable).group(0)
variable = re.sub(" {2,}", " ", variable)开发者_开发问答
variable = re.sub("'{2,}", "'", variable)
variable = re.sub("\-{2,}", "-", variable)

Kind Regards,

Marian


This is a start:

variable = re.search("[a-zA-Z0-9]+[a-zA-Z0-9\-' ]+[a-zA-Z0-9]+", variable).group(0)
variable = re.sub("([ '\-])\\1+", "\\1", variable)
0

精彩评论

暂无评论...
验证码 换一张
取 消