类似 PowerShell 的格式化

发布于 2025-01-12 18:52:41 字数 226 浏览 3 评论 0原文

我正在尝试测试字符串是否采用两种格式之一:标题(年份)或标题(年份)[Addl]

对于第一个,这有效: if ($name -like "* (*)") ...

对于第二个,这不起作用: if ($name -like "* (*) [*]")...

我知道括号使它认为它是一个正则表达式。如何使其不是正则表达式?

感谢您的帮助!

I am trying to test if a string is in one of two formats: Title (Year) or Title (Year) [Addl]

For the first, this works: if ($name -like "* (*)")...

For the second, this does not work: if ($name -like "* (*) [*]")...

I know the brackets make it think it is a regular expression. How do I make it not a regular expression?

Thanks for your assistance!!!

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

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

发布评论

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

评论(1

乖乖公主 2025-01-19 18:52:41

对于通配符表达式< /a> 在 PowerShell 中,转义字符是反引号 `

'Title (Year)' -like '* (*)'             # => True
'Title (Year) [Addl]' -like '* (*) `[*]' # => True

测试这一点的一种方法是使用 [WildcardPattern]::Escape(..)

[WildcardPattern]::Escape('[]') # => `[`]

如果您想使用以下命令测试模式正则表达式 你会使用-match 运算符,转义字符将是反斜杠 \

'Title (Year)' -match '^\w+\s\(\w+\)

并且,如 briantist 的有用评论指出,您可以使用 [regex]::Escape(..) 转义正则表达式特殊字符:

[regex]::Escape('()[]') # => \(\)\[]
# => True 'Title (Year) [Addl]' -match '^\w+\s\(\w+\)\s\[\w+]

并且,如 briantist 的有用评论指出,您可以使用 [regex]::Escape(..) 转义正则表达式特殊字符:


 # => True

并且,如 briantist 的有用评论指出,您可以使用 [regex]::Escape(..) 转义正则表达式特殊字符:

For Wildcard expressions in PowerShell, the escape character is the backtick `:

'Title (Year)' -like '* (*)'             # => True
'Title (Year) [Addl]' -like '* (*) `[*]' # => True

One way to test this is using [WildcardPattern]::Escape(..):

[WildcardPattern]::Escape('[]') # => `[`]

If you want to test the pattern using regular expressions you would be using the -match operator, and the escape character would be the backslash \:

'Title (Year)' -match '^\w+\s\(\w+\)

And, as briantist's helpful comment points out, you can use [regex]::Escape(..) to escape regex special characters:

[regex]::Escape('()[]') # => \(\)\[]
# => True 'Title (Year) [Addl]' -match '^\w+\s\(\w+\)\s\[\w+]

And, as briantist's helpful comment points out, you can use [regex]::Escape(..) to escape regex special characters:


 # => True

And, as briantist's helpful comment points out, you can use [regex]::Escape(..) to escape regex special characters:

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