[英]Using Regex In Python To Find Substring?
I have A string:我有一个字符串:
line_to_test = "http://website/[SequenceOfLetters&NumbersONLY].html"
I want a regex for matching the above pattern:我想要一个正则表达式来匹配上述模式:
what i have tried currently is:我目前尝试过的是:
c = len(re.findall(r"http:\/\/website\/([a-zA-Z0-9]?).html",line_to_test))
But c
here comes to be null even when the line_to_test
contains the pattern.但是即使line_to_test
包含模式,这里的c
也会为空。
?
means whatever preceded it was optional, in this case [a-zA-Z0-9]
.意味着它之前的任何内容都是可选的,在这种情况下是[a-zA-Z0-9]
。 That means you can have a letter or number either 0
or 1
times.这意味着您可以有0
次或1
次的字母或数字。
You should use the *
, to select it 0
times or more, or use the +
, to select it 1
times` or more.您应该使用*
, 选择它0
次或更多次,或使用+
, 选择它1
次或更多。
Try this RegEx:试试这个正则表达式:
c = len(re.findall(r"http:\/\/website\/([a-zA-Z0-9]+).html",line_to_test))
If you used a *
, it would be the same as ([a-zA-Z0-9]+)?
如果您使用*
,它将与([a-zA-Z0-9]+)?
, meaning http://website/.html
would work. ,这意味着http://website/.html
可以工作。
?
will only match 0 or 1 character.只会匹配 0 或 1 个字符。 Try尝试
c = len(re.findall(r"http:\/\/website\/([a-zA-Z0-9]+).html",line_to_test))
You can use an online service like regexr to test your regexes: http://regexr.com/3d301您可以使用像 regexr 这样的在线服务来测试您的正则表达式: http ://regexr.com/3d301
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.