2

我正在尝试在字符串中搜索某个值并将其替换为另一个值。

这个例子:

细绳:"/accounts/{accountId}/cheques/{chequeId}/cancel"

我正在尝试用数字 1 替换 { 和 } 之间的任何内容。

所以我最终会得到:

细绳:"/accounts/1/cheques/1/cancel"

我正在使用以下内容:

prepedURI = System.Text.RegularExpressions.Regex.Replace(prepedURI, "{.*}", "1")

但不幸的是,替换函数正在返回:字符串:“/accounts/1/cancel”

它似乎忽略了第一个 } 并替换了第二个 } 之前的所有内容。

有什么建议吗?

原谅我的笨蛋。这是我的第一次正则表达式体验,我正在尽我所能理解模式中的所有这些“标志”。

示例(您可以粘贴到按钮单击事件中以了解我的意思):

Dim prepedURI As String = "/accounts/{accountId}/cheques/{chequeId}/cancel"
prepedURI = System.Text.RegularExpressions.Regex.Replace(prepedURI, "{.*}", "1")
MsgBox(prepedURI)
4

1 回答 1

1

利用 ?在右花括号之前

{.*?}

工作示例可以在以下链接中找到 - http://rubular.com/r/1LpnGNC3sC

于 2013-09-10T00:16:43.100 回答