我正在尝试解析我的VHDL代码以进行一些额外的检查。
我正在寻找一个正则表达式来检查VHDL中的相关标识符。而且我仍然是正则表达式的新手。
它有以下规则:
只能包含字母(A..Z a..z)数字(0..9)和下划线('_')
必须以字母
可能不会以下划线字符结尾
可能不包含两个连续的下划线字符
所以我目前的问题是要检查两个连续的下划线字符......
更新:我想我自己刚刚回答了这个问题......请仔细检查
[A-Za-z](_?[A-Za-z0-9])*
答案 0 :(得分:0)
(?!.*__)[a-zA-Z][\w]*[^_]
这应该可以解决问题。
说明:
# (?!.*__)[a-zA-Z][\w]*[^_]
#
# Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*__)»
# Match any single character that is not a line break character «.*»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the characters “__” literally «__»
# Match a single character present in the list below «[a-zA-Z]»
# A character in the range between “a” and “z” «a-z»
# A character in the range between “A” and “Z” «A-Z»
# Match a single character that is a “word character” (letters, digits, etc.) «[\w]*»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match any character that is NOT a “_” «[^_]»
答案 1 :(得分:0)
在您对问题的更新中,建议您:{letter}({underscore}?{letter_or_digit})*
。这正是VHDL规范为表达式所建议的内容。值得注意的是,基本标识符不区分大小写。也就是说,id
和ID
被视为具有相同的标识符。
但是,在VHDL中,还有扩展标识符。适合这些的正则表达式是:
({backslash}{Any ISO 8859-1 except backslash}*{backslash})+
另请注意,传统上不处理以下标识符,而是保留字:这是2002年规范中的列表。根据您实施的规范版本,可能会有更多或更少的保留字。
abs access after alias all and architecture array assert attribute begin block
body buffer bus case component configuration constant disconnect downto else
elsif end entity exit file for function generate generic group guarded if
impure in inertial inout is label library linkage literal loop map mod nand
new next nor not null of on open or others out package port postponed
procedural procedure process protected pure range record reference register
reject rem report return rol ror select severity shared signal sla sll sra srl
subtype then to transport type unaffected units until use variable wait when
while with xnor xor
值得注意的是,在VHDL [A-Za-z]
中并不是字母表中的所有字母。您还应该包含ISO 8859-1拉丁字符。您可以找到有关这些字符here的更多信息。
但是,话虽如此,这里是额外的大写字母:
À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß
以下是额外的小写字母:
à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ