开发者

Regex for Multiple Key:Value Search Terms

I need a .NET (C#) regular expression for parsing a string of search terms. The terms are key:value pairs and are delimited by spaces. The thing that's throwing me for a loop is the fact that the key:value pairs may have sp开发者_如何转开发aces in the value.

Here's an example string:

f:john l:smith c:san francisco st:ca

I expect to get back the following terms:

  f:john
  l:smith
  c:san francisco
  st:ca

Any help? Thanks.


I think that this one will work. It uses a lookahead to make sure that the last word doesn't have a : terminating it.

\b\w+:[\w\s]+\b(?!:)


This is my try:

([\w]+)\:([\w\s]+)\s(?=([\w]+)\:)?

2 caveats:

  • Each match will have three captures in it. Ignore the last one.
  • The input text must have a space at the end.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜