String.Format 的正则表达式

发布于 2024-08-09 22:17:09 字数 376 浏览 7 评论 0原文

大家好! :)

我有一个应用程序,用户可以在其中将字符串插入文本框,稍后将用于 String.Format 输出。因此,用户的输入必须具有某种格式:

我想仅替换一个占位符,因此字符串应采用如下形式:“Text{0}Text”。因此它必须至少包含一个“{0}”,但大括号之间不能有其他语句,例如没有 {1}。

对于“{0}”之前和之后的文本,我允许使用任何字符。

所以我想,我必须遵守以下限制:{必须写成{{,}必须写成}},“必须写成\”和\必须写成\。

有人可以告诉我,如何编写这样的正则表达式吗?特别是,我可以执行“任何字符没有”之类的操作来排除上面的四个字符( {、}、" 和 \ ),而不是列出每个允许的字符吗?

非常感谢! 尼基:)

Hiho everyone! :)

I have an application, in which the user can insert a string into a textbox, which will be used for a String.Format output later. So the user's input must have a certain format:

I would like to replace exactly one placeholder, so the string should be of a form like this: "Text{0}Text". So it has to contain at least one '{0}', but no other statement between curly braces, for example no {1}.

For the text before and after the '{0}', I would allow any characters.

So I think, I have to respect the following restrictions: { must be written as {{, } must be written as }}, " must be written as \" and \ must be written as \.

Can somebody tell me, how I can write such a RegEx? In particular, can I do something like 'any character WITHOUT' to exclude the four characters ( {, }, " and \ ) above instead of listing every allowed character?

Many thanks!!
Nikki:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆萌少年 2024-08-16 22:17:09

我讨厌成为那个不回答问题的人,但实际上要求用户格式化输入以使用 String.Format 的可用性很差。向他们提供两个输入请求,以便他们输入 {0} 之前的部分和 {0} 之后的部分。然后您将只想连接字符串而不是使用 String.Format - 在用户提供的文本上使用 String.Format 只是一个坏主意。

I hate to be the guy who doesn't answer the question, but really it's poor usability to ask your user to format input to work with String.Format. Provide them with two input requests, so they enter the part before the {0} and the part after the {0}. Then you'll want to just concatenate the strings instead of use String.Format- using String.Format on user-supplied text is just a bad idea.

绿光 2024-08-16 22:17:09
[^(){}\r\n]+\{0}[^(){}\r\n]+

将匹配除 (, )、{, } 和换行符之外的任何文本,然后匹配 {0},然后与之前相同。 {0}前后至少各有一个字符;如果您不希望这样,请将 + 替换为 *

您可能还想将正则表达式锚定到输入字符串的开头和结尾:

^[^(){}\r\n]+\{0}[^(){}\r\n]+$
[^(){}\r\n]+\{0}[^(){}\r\n]+

will match any text except (, ), {, } and linebreaks, then match {0}, then the same as before. There needs to be at least one character before and after the {0}; if you don't want that, replace + with *.

You might also want to anchor the regex to beginning and end of your input string:

^[^(){}\r\n]+\{0}[^(){}\r\n]+$
与之呼应 2024-08-16 22:17:09

(类似于蒂姆的回答)

类似:

^[^{}()]*(\{0})[^{}()]*$

http://www.regular-expressions.info 进行测试/javascriptexample.html

(Similar to Tim's answer)

Something like:

^[^{}()]*(\{0})[^{}()]*$

Tested at http://www.regular-expressions.info/javascriptexample.html

时光无声 2024-08-16 22:17:09

听起来您正在寻找 [^CHARS_GO_HERE] 构造。您需要的确切正则表达式取决于您的正则表达式引擎,但它类似于 [^({})]

查看Regular-Expressions.info 上的字符类页面。

It sounds like you're looking for the [^CHARS_GO_HERE] construct. The exact regex you'd need depends on your regex engine, but it would resemble [^({})].

Check out the "Negated Character Classes" section of the Character Class page at Regular-Expressions.info.

酒浓于脸红 2024-08-16 22:17:09

我认为您的问题可以通过正则表达式来回答:

^(((\{\{|\}\}|\\"|\\\\|[^\{\}\"\\])*(\{0\}))+(\{\{|\}\}|\\"|\\\\|[^\{\}\"\\])*$

解释:

表达式的构建方式如下:

^(allowed chars {0})+(allowed chars)*$

一个或多个允许的字符序列,后跟一个 {0},末尾带有可选的允许的字符。

allowed chars 由您提到的 4 个序列组成(我假设 \ escape 是 \\ 而不是 \。)加上所有不包含转义字符的字符:

(\{\{|\}\}|\\"|\\\\|[^\{\}\"\\])

组合起来它们组成了我开始使用的正则表达式。

I think your question can be answered by the regexp:

^(((\{\{|\}\}|\\"|\\\\|[^\{\}\"\\])*(\{0\}))+(\{\{|\}\}|\\"|\\\\|[^\{\}\"\\])*$

Explanation:

The expression is built up as follows:

^(allowed chars {0})+(allowed chars)*$

one or more sequences of allowed chars followed by a {0} with optional allowed chars at the end.

allowed chars is built of the 4 sequences you mentioned (I assumed the \ escape is \\ instead of \.) plus all chars that do not contain the escapes chars:

(\{\{|\}\}|\\"|\\\\|[^\{\}\"\\])

combined they make up the regexp I started with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文