开发者

Regular Expressions (.NET) - How can I match a pattern that contains a variable number of digits at the end of the string?

string src = "portfolio1, portfol开发者_如何学编程io2, portfolio20, portfolio300";

I'd like to match all strings that are of the pattern @"portfolio\d" where \d can be anywhere from 1-3 digits in length. I have read that the use of {a, b} should work, so I tried:

pattern = @"portfolio\d{1, 3}"

Searching in the string, src, for this pattern returned an empty set. the following patterns worked partially:

pattern = @"portfolio\d"
pattern = @"portfolio\d{1}"


Try this:

pattern = @"portfolio\d{1,3}"

Note that you should not put a space in between the brackets, as you have in your example. That's why it didn't work right.


String pattern = @"^(?:(?:portfolio\d{1,3})(?:\x2C\s)*)+$";

My attempt. Will match any number of comma-seperated portfolio\d{1,3}'s


Running pattern "portfolio\d{1,3}" in Expresso, I get 4 matches on each of the portfolios. The space did it was the key.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜