简体   繁体   English

C# 正则表达式匹配 url 末尾的模式

[英]C# Regex that matches a pattern at the end of an url

I need a regex that matches a pattern for urls with the end like -p123456.html , so the end it will have a hyphen and the letter p followed by a sequence of numbers right before .html .我需要一个正则表达式,它与结尾的 url 模式匹配-p123456.html ,所以它的结尾将有一个连字符和字母p ,然后是.html之前的数字序列。

Here are some examples:这里有些例子:

Can anyone help me with that?任何人都可以帮助我吗?

This will work for your case: .+\-p\d+\.html$这将适用于您的情况: .+\-p\d+\.html$

Note: You could expand this to do more url validation, but this is quick and easy if you know your input is always going to be a url.注意:你可以扩展它来做更多的 url 验证,但如果你知道你的输入总是url,这将是快速和简单的。

Explanation:解释:

.+ - wildcard to start the string. .+ - 通配符开始字符串。 Matches all up until the -p you care about匹配所有直到您关心的-p

\-p - matches literal character '-' and 'p' . \-p - 匹配文字字符'-''p' Note: you don't have to escape the dash, but I like to because I don't want to confuse it with ranges such as [az] .注意:您不必逃避破折号,但我喜欢这样做,因为我不想将它与诸如[az]之类的范围混淆。 You could use -p here.你可以在这里使用-p

\d+ - your series of characters (1 or more) \d+ - 你的一系列字符(1个或更多)

\.html - escaping the dot ( '.' ) is important here so it's not just a wildcard. \.html - escaping 点( '.' )在这里很重要,所以它不仅仅是通配符。 This matches literal '.html'这匹配文字'.html'

$ - matches the end of the string so you know the url ends with -p1234.html $ - 匹配字符串的结尾,所以你知道 url 以-p1234.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM