正则表达式匹配字符串之间的字符串

时间:2020-06-29 06:28:09

标签: javascript python regex string match

我一直很难找到正则表达式。对不起,我不得不问,

https://meet.google.com/zzz-tssd-trt?authuser=1
https://meet.google.com/zzz-tssd-trt
https://meet.google.com/zzz44-5454ws-fgaf

如何从这些字符串中获取代码,例如zzz44-5454ws-fgafzzz-tssd-trtzzz-tssd-trt

3 个答案:

答案 0 :(得分:2)

无需使用正则表达式。使用URL

更安全

const paths = `https://meet.google.com/zzz-tssd-trt?authuser=1
https://meet.google.com/zzz-tssd-trt
https://meet.google.com/zzz44-5454ws-fgaf`
 .split(/\n/).map(url => new URL(url).pathname)

console.log(paths)

答案 1 :(得分:1)

您可以使用正则表达式\w(?=/)/([^?|\n]+),此regwx会将最后一个/之后,?或换行之前的所有字符分组。

import re

s = """
https://meet.google.com/zzz-tssd-trt?authuser=1
https://meet.google.com/zzz-tssd-trt
https://meet.google.com/zzz44-5454ws-fgaf
"""

print(re.findall(r"\w(?=/)/([^?|\n]+)", s))

输出

['zzz-tssd-trt', 'zzz-tssd-trt', 'zzz44-5454ws-fgaf']

答案 2 :(得分:0)

'https://meet.google.com/[a-z0-9]{3}-[a-z0-9]{4}-[a-z0-9]{3}','gi' 这更具限制性,对我来说效果很好。