首页 > 解决方案 > 有人可以帮我理解这个正则表达式在做什么吗?

问题描述

有人可以分解这个正则表达式匹配的内容吗?

Regex.Match("<a>", "^<([a-zA-Z][a-zA-Z0-9]*)( [^>]*)?>$")

标签: .netregexvb.net

解决方案


给你,解释和格式化

 ^                             # The beginning of the string BOS
 <                             # A literal '<'
 (                             # (1 start), Capture group 1
      [a-zA-Z]                      # Start with a letter
      [a-zA-Z0-9]*                  # 0 or more letter or number
 )                             # (1 end)
 (                             # (2 start) Optional Capture group 2
      [^>]*                         # 0 or more, non '>' character
 )?                            # (2 end)
 >                             # A literal '>'
 $                             # The end of the string EOS

一个忠告,这个结构有它的位置([^>]*)?
,但它应该写成 this ([^>]*?)


推荐阅读