regex - 用于用户输入验证的 Azure Blob 存储正则表达式模式

标签 regex azure powershell azure-blob-storage blob

我在 PowerShell (.NET Framework) 中开发了一个函数,用于从任何给定的 Azure Blob 存储中检索数据:到目前为止,一切顺利。

不幸的是,当涉及到验证用户的输入时,我不能依赖外部模块或库,例如 NameValidator Class适用于 .NET 的 Azure SDK。

尽管如此,文章Naming and Referencing Containers, Blobs, and Metadata详细介绍了命名规则,因此正则表达式模式可能会派上用场。

对于容器名称,我想出了这个,它似乎很合适:

(?=^.{3,63}$)(?!.*--)[^-][a-z0-9-]*[^-]

容器名称

A container name must be a valid DNS name, conforming to the following naming rules:

  • Container names must start or end with a letter or number, and can contain only letters, numbers, and the dash (-) character.
  • Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.
  • All letters in a container name must be lowercase.
  • Container names must be from 3 through 63 characters long.

对于Blob名称,但是我无法绕过路径段的计数:

(?=^.{1,1024}$)(?<=^|\/)(\S*?)[^\.] (?=\/|$)

注意:Azure 存储模拟器已被弃用,因此超出了范围。

Blob 名称

A blob name must conforming to the following naming rules:

  • A blob name can contain any combination of characters.
  • A blob name must be at least one character long and cannot be more than 1,024 characters long, for blobs in Azure Storage.
  • The Azure Storage emulator supports blob names up to 256 characters long. For more information, see Use the Azure storage emulator for development and testing.
  • Blob names are case-sensitive.
  • Reserved URL characters must be properly escaped.
  • The number of path segments comprising the blob name cannot exceed 254. A path segment is the string between consecutive delimiter characters (e.g., the forward slash '/') that corresponds to the name of a virtual directory.
  • Note Avoid blob names that end with a dot (.), a forward slash (/), or a sequence or combination of the two. No path segments should end with a dot (.).

The Blob service is based on a flat storage scheme, not a hierarchical scheme. However, you may specify a character or string delimiter within a blob name to create a virtual hierarchy. For example, the following list shows valid and unique blob names. Notice that a string can be valid as both a blob name and as a virtual directory name in the same container:

/a
/a.txt
/a/b
/a/b.txt

You can take advantage of the delimiter character when enumerating blobs.

注意:就在问这个问题之前,我发现这个问题可以回答我自己已经解决的问题或使用上述类:

Azure Container Name RegEx

How to validate Azure storage blob names

顺便问一下,有人知道 PowerShell 使用哪种风格的正则表达式吗?

最佳答案

您需要使用

^(?!.{1025})/?[^/]*[^/.](?:/[^/]*[^/.]){0,253}$
^(?=.{1,1024}$)/?[^/]*[^/.](?:/[^/]*[^/.]){0,253}$

请参阅regex demo .

详细信息:

  • ^ - 字符串开头
  • (?=.{1,1024}$) - 字符串应包含 1 到 1024 个字符
  • (?!.{1025}) - 字符串包含的字符不能超过 1025 个
  • /? - 可选的 /
  • [^/]*[^/.] - 除 / 之外的零个或多个字符,然后是 / 以外的字符以及
  • (?:/[^/]*[^/.]){0,253} - 出现 0 到 253 次 /,后跟零个或多个除/ 然后是 / 之外的字符。
  • $ - 字符串结尾。

关于regex - 用于用户输入验证的 Azure Blob 存储正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72040722/

相关文章:

powershell - conda 在 powershell 上激活

regex - R 正则表达式错误 :java. lang.NoSuchMethodException 中的 Mallet:给定参数没有合适的方法

密码强度验证的 RegEx 问题

Python:如何使用正则表达式将句子拆分为新行,然后使用空格将标点符号与单词分开?

azure - 跨项目共享 azure devops 变量

powershell - 如何通过脚本查看虚拟硬盘的分区数?

powershell - 如何在win10命令提示符下使用ffmpeg连接来自不同子目录的视频文件?

Python 正则表达式插入符号在多行模式下不起作用?

azure - 出现错误 "Cannot perform a single-role upgrade using different versions of the SDK"

c# - Azure函数: Blob identifiers must be in the format 'container/blob'